Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"a bin target must be available for 'cargo run'"

While building a new Rust "Project from other sources", in Intellij IDEA 2017, I was unable to run the project through its UI.

C:/Users/sjsui/.cargo/bin/cargo.exe run error: a bin target must be available for cargo run

Process finished with exit code 101

I noticed that no --bin target was provided by my build configuration so I placed the path to the projects target folder; same result.

enter image description here

C:/Users/sjsui/.cargo/bin/cargo.exe run --bin C:\Users\sjsui\exercism\rust\hello-world\target\debug error: no bin target named C:\Users\sjsui\exercism\rust\hello-world\target\debug

I tried creating a fresh Rust project through the Cargo command line interface, and received this error when running it:

error: could not exec the linker link.exe: The system cannot find the file specified. (os error 2) note: the msvc targets depend on the msvc linker but link.exe was not found

note: please ensure that VS 2013 or VS 2015 was installed with the Visual C++ option

Evidently I must install Visual C++ build tools 2017 and am in the process of doing so. Are these errors related, or different issues?

like image 257
dcdgo Avatar asked Feb 25 '18 23:02

dcdgo


Video Answer


1 Answers

By default, Cargo will consider the file src/main.rs to be the main binary target for the package. If this file doesn't exist, and there are no other binary targets defined in Cargo.toml, you'll get this error.

According to the documentation, when you create a Rust project in IntelliJ IDEA, you get an option to Use a binary (application) template. This should give you a src/main.rs instead of a src/lib.rs (which is the default root file for a library target). Using Cargo on the command line, you can also create an application package with cargo new hello.

Cargo defaults to --bin to make a binary program. To make a library, we'd pass --lib.

When you use --bin on the cargo run command, the argument refers to one of the [[bin]] sections in Cargo.toml, or files following the pattern src/bin/*.rs (the argument replaces the *) if there are no [[bin]] sections in Cargo.toml. For example, cargo run --bin foo will either compile and run src/bin/foo.rs or the [[bin]] section with name = "foo" in Cargo.toml.

like image 67
Francis Gagné Avatar answered Sep 21 '22 08:09

Francis Gagné