代码之家  ›  专栏  ›  技术社区  ›  Lukas Kalbertodt

如何在货物工作区的根目录中指定默认运行哪个板条箱的“货物运行”?

  •  8
  • Lukas Kalbertodt  · 技术社区  · 6 年前

    现在我有一个有三个成员的货舱。

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

    如果我跑 cargo run 在根目录中,出现以下错误:

    错误 :清单路径 /home/lukas/dev/mahboi/Cargo.toml 是虚拟清单,但此命令需要在此工作区中针对实际包运行

    这是有道理的。我能跑 cargo run -p foo 而且有效。但问题是: foo 是唯一一个可执行的板条箱,我会经常执行它,所以如果我可以运行 货物运输 执行它。

    我试着用 default-members 但这没用:

    default-members = ["foo"]
    

    有别的方法告诉货物吗 货物运输 应该执行 板条箱(相当于运行 货物运输 foo/ 子目录?我也会接受使根板条箱非虚拟的答案(即添加 [package] 键)。

    1 回复  |  直到 6 年前
        1
  •  11
  •   Lukas Kalbertodt    5 年前

    单二进制

    这个 is available as of Rust 1.30 . 以下是我使用的完整文件集:

    货物.toml

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

    foo/货物.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!");
    }
    

    酒吧/货物.toml

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

    钢筋/钢筋混凝土/lib.rs

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

    baz/货物.toml

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

    baz/src/lib.rs公司

    #[cfg(测试)]
    mod测试{
    #[测试]
    fn它有效(){
    断言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!
    

    多个二进制文件

    从Rust1.37.0开始,您可以使用Cargo的“默认运行”功能来指定要使用哪一个。

    foo/货物.toml

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