Cargo run --release in a workspace

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
1 Like

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.

3 Likes

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.