What is the defined behaviour for cargo run --release
in a workspace.
Does it (1) run release in all the bin
crates ? (2) just the first bin
crate ? or (3) some other file controls behaviour ?
What is the defined behaviour for cargo run --release
in a workspace.
Does it (1) run release in all the bin
crates ? (2) just the first bin
crate ? or (3) some other file controls behaviour ?
As far as I know, if the workspace is meanwhile a package (bin crate), cargo run --release
will build and run the package.
For example, if you have a Cargo.toml like this:
[package]
name = "one-bin-crate"
[workspace]
members = ["another-crate"]
and the dir/file structure is:
.
āāā another-crate
ā āāā Cargo.toml
ā āāā src
ā āāā main.rs
āāā Cargo.toml
āāā src
ā āāā main.rs
and the current path is where the workspace Cargo.toml lies,
then cargo run --release
means cargo run -p one-bin-crate --release
.
However, if your workspace & package has multiple bins:
.
āāā another-crate
ā āāā Cargo.toml
ā āāā src
ā āāā main.rs
āāā Cargo.lock
āāā Cargo.toml
āāā src
ā āāā bin
ā ā āāā tmp.rs
ā āāā main.rs
then an error will be throwed:
$ cargo run --release
error: `cargo run` could not determine which binary to run. Use the `--bin` option to specify a binary,or the `default-run` manifest key.
available binaries: one-bin-crate, tmp
luckily cargo clearly tells you how to do:
# the bin name of src/main.rs is known as the package name
$ cargo run --bin one-bin-crate --release
# or
$ cargo run --bin tmp --release
Related cmds:
# you can only run one binary one time, see help
$ cargo run --help
# but you can build multiple binarys one time, see help
$ cargo build --help
If your workspace has a virtual manifest with multiple binary crates, you can use the default-members
key to change which package cargo run
will choose by default. However, beware that this also affects other Cargo commands.
[workspace]
members = ["a", "b"]
default-members = ["a"]
For non-virtual packages that contain multiple binaries, you can use the default-run
field to alter the default behavior.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.