"a bin target must be available for 'cargo run'"
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
.