我如何在 Cargo 工作区的根目录中指定默认情况下哪个 crate `cargo 运行` 运行s?

How can I specify which crate `cargo run` runs by default in the root of a Cargo workspace?

现在我有一个包含三个成员的 Cargo 工作区。

[workspace]
members = [
    "foo",
    "bar",
    "baz",
]

如果我在根目录下运行 cargo run,我得到这个错误:

error: manifest path /home/lukas/dev/mahboi/Cargo.toml is a virtual manifest, but this command requires running against an actual package in this workspace

这是有道理的。我可以 运行 cargo run -p foo 并且有效。但问题是:foo 是唯一可执行的箱子,我会经常执行它,所以如果我可以 运行 cargo run 并执行它会很好。

我尝试使用 default-members 键,但这没有帮助:

default-members = ["foo"]

是否有另一种方式告诉 Cargo cargo run 应该执行 foo crate(相当于 运行ning cargo runfoo/ 子目录中) ?我也会接受使根箱非虚拟的答案(即添加 [package] 键)。

单个二进制文件

这个is available as of Rust 1.30。这是我测试过的完整文件集:

Cargo.toml

[workspace]
members = [
    "foo",
    "bar",
    "baz",
]

foo/Cargo.toml

[package]
name = "foo"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]

[dependencies]

foo/src/main.rs

fn main() {
    println!("Hello, world!");
}

bar/Cargo.toml

[package]
name = "bar"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]

[dependencies]

bar/src/lib.rs

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

baz/Cargo.toml

[package]
name = "baz"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]

[dependencies]

baz/src/lib.rs

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}
$ tree .
.
├── Cargo.lock
├── Cargo.toml
├── bar
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── baz
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── foo
│   ├── Cargo.toml
│   └── src
│       └── main.rs
├── src
│   └── lib.rs
└── target
    └── ...
$ cargo run
   Compiling baz v0.1.0 (file:///private/tmp/example/baz)
   Compiling bar v0.1.0 (file:///private/tmp/example/bar)
   Compiling foo v0.1.0 (file:///private/tmp/example/foo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s
     Running `target/debug/foo`
Hello, world!

多个二进制文件

从 Rust 1.37.0 开始,您可以使用 Cargo 的 "default-run" 功能来指定使用哪一个。

foo/Cargo.toml

[package]
name = "foo"
version = "0.0.1"
authors = ["An Devloper <an.devloper@example.com>"]
default-run = "foo"