[英]How can I specify which crate `cargo run` runs by default in the root of a Cargo workspace?
Right now I have a Cargo workspace with three members. 现在我有一个有三个成员的Cargo工作区。
[workspace]
members = [
"foo",
"bar",
"baz",
]
If I run cargo run
in the root directory, I get this error: 如果我在根目录中运行
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错误 :清单路径
/home/lukas/dev/mahboi/Cargo.toml
是虚拟清单,但此命令需要针对此工作空间中的实际包运行
That makes sense. 那讲得通。 I can run
cargo run -p foo
and it works. 我可以运行
cargo run -p foo
,它的工作原理。 But the thing is: foo
is the only crate that is executable and I will execute it very often, so it would be nice if I could just run cargo run
and execute it. 但问题是:
foo
是唯一可执行的包,我会经常执行它,所以如果我可以运行cargo run
并执行它会很好。
I tried to use the default-members
key, but this didn't help: 我试图使用
default-members
密钥,但这没有帮助:
default-members = ["foo"]
Is there another way to tell Cargo that cargo run
should execute the foo
crate (equivalent to running cargo run
in the foo/
subdirectory)? 是否还有另一种方法可以告诉Cargo
cargo run
应该执行foo
crate(相当于在foo/
子目录中运行cargo run
)? I would also accept answers that make the root crate non virtual (ie add a [package]
key). 我也会接受让root crate非虚拟的答案(即添加
[package]
键)。
This is available as of Rust 1.30 . 这可以从Rust 1.30开始提供 。 Here is the complete set of files I tested with:
这是我测试过的完整文件集:
Cargo.toml Cargo.toml
[workspace]
members = [
"foo",
"bar",
"baz",
]
foo/Cargo.toml 富/ Cargo.toml
[package]
name = "foo"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]
[dependencies]
foo/src/main.rs 富/ SRC / main.rs
fn main() {
println!("Hello, world!");
}
bar/Cargo.toml 酒吧/ Cargo.toml
[package]
name = "bar"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]
[dependencies]
bar/src/lib.rs 酒吧/ SRC / lib.rs
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
baz/Cargo.toml 巴兹/ Cargo.toml
[package]
name = "baz"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]
[dependencies]
baz/src/lib.rs 巴兹/ 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!
As of Rust 1.37.0 you can use Cargo's "default-run" feature to specify which one to use. 从Rust 1.37.0开始,您可以使用Cargo的“默认运行”功能来指定使用哪一个。
foo/Cargo.toml 富/ Cargo.toml
[package]
name = "foo"
version = "0.0.1"
authors = ["An Devloper <an.devloper@example.com>"]
default-run = "foo"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.