48

I am new to Rust and attempting to build a test project with Cargo. My Cargo.toml looks like:

[package]
name = "rust-play"
version = "0.0.1"
authors = [ "Bradley Wogsland <omitted>" ]

(but the actual TOML file doesn't omit my email). When I cargo build I am getting the following error:

error: failed to parse manifest at /Users/wogsland/Projects/rust-play/Cargo.toml

Caused by: no targets specified in the manifest either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present

My main function is in a src/test.rs file. Do I need to specify that in the TOML file? If so, how? I tried adding

target = "src/test.rs"

to no avail.

7 Answers 7

64

As the error says:

either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present

So the direct answer is to add a [[bin]] section:

[[bin]]
name = "test"
path = "src/test.rs"

However, it's far more usual to just place the file in the expected location: src/main.rs. You could also place it in src/bin/test.rs if you plan on having multiple binaries.

If it's actually for testing your code, then unit tests go in the same file as the code they are testing and integration tests go in tests/foo.rs.

3
  • 1
    I had the problem while installing IntelliJ IDEA and it being confused on the matter, but this solved the intellij idea error. Commented May 21, 2019 at 19:15
  • strange..i have the toml in /src and i have main.rs in that /sr and it still doesnt find it. i get the same error without adding a 'bin' section
    – mike01010
    Commented May 11 at 21:38
  • this works for me, but since i am learning and documenting my progress in different source files under the src dir, i need a way to specify all the paths for each file such that i do not have to change the path value when i am working with a particular file
    – Brian Obot
    Commented May 15 at 7:40
17

Alternative issue and solution: You can also be faced with this error if you have copied Cargo.toml file to a parent folder of the crate.

2
  • Why is that? Is this a bug? Commented Oct 15, 2021 at 9:32
  • 1
    No, not a bug, cargo tries to find the root of the project. It works this way to allow you to run cargo command in subcrate's folder of a workspace. Commented Oct 15, 2021 at 22:31
3

In my case and probably in your case as well, the rs file was not named main.rs while Cargo assumes that src/main.rs is the crate root of a binary crate. So, the rule is that If project is an executable, name the main source file src/main.rs. If it is a library, name the main source file src/lib.rs.

Additionally, Cargo will also treat any files located in src/bin/*.rs as executables like mentioned in the previous answer.

2
  • Can you expand a bit more to highlight what is different about this answer as opposed to the other answer? The other answer already says "place the file in the expected location: src/main.rs"
    – Shepmaster
    Commented May 30, 2016 at 1:14
  • 1
    @Shepmaster Thanks for asking. In my answer, I tried to explicitly highlight the 'renaming' of file to main.rs if someone is using any other name but still has it in location: srs. Something which was not clearly mentioned in your answer that it has to be main.rs file and no other name. Also, felt imp to mention about library. Can I request to take back the negative vote? :) Commented Aug 23, 2016 at 14:28
2

I ran into this issue on Ubuntu 20.04 after having inadvertently copied Cargo.toml to my home folder. Even though my working directory had a properly defined Cargo.toml, the copy in $HOME was taking precedence and causing builds to fail.

2

I also had this issue and it was because the parent directory also contained a Cargo.toml file and it was prioritising that over the one in the current directory

1

As a summary:

If you use cargo new xxx --bin, you will find the file in the src directory is named main.rs. And when you check the file Cargo.toml. It is the same as you written. So the first way is to change the file in src to main.rs

As the cargo report, we can use the [[bin]] to set the file. @Shepmaster has solved it.

Both two ways can work.

2
  • Can you explain what new information this answer provides compared to the existing answer?
    – Shepmaster
    Commented Aug 22, 2016 at 11:14
  • All right, I just found that your answer has explained both of two ways.@Shepmaster, Actually, I am new to rust, and I encountered the same problem, so I posted that answer when i solved it.
    – Jesse Chen
    Commented Aug 23, 2016 at 6:47
1

I did stumble over this problem today. But this time it was caused by an invalid cargo.toml somewhere in the parent directory hierarchy.

Even if at the root mount there is an invalid /cargo.toml file , all builds at that server will fail.

The workaround, proposed in this forum topic https://users.rust-lang.org/t/what-is-the-correlation-between-two-cargo-toml-in-the-file-hierarchy/101933, was to turn the cargo.toml file into a standalone workspace, by adding a [workspace] section in the cargo.toml file.

Not the answer you're looking for? Browse other questions tagged or ask your own question.