46

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

61

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.

2
  • Thank you, this was very helpful!
    – wogsland
    May 27, 2016 at 20:10
  • 1
    I had the problem while installing IntelliJ IDEA and it being confused on the matter, but this solved the intellij idea error. May 21, 2019 at 19:15
16

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.

3
  • 4
    Thanks a lot, I'd never think about that, solved the problem here.
    – Rodolfo
    Jul 8, 2019 at 15:16
  • Why is that? Is this a bug? 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. Oct 15, 2021 at 22:31
2

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
    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? :) 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.

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
    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
    Aug 23, 2016 at 6:47
1

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

0

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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