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

后端 未结 1 1002
鱼传尺愫
鱼传尺愫 2021-02-08 02:54

Right now I have a Cargo workspace with three members.

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

If I run cargo ru

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-08 03:41

    Single Binary

    This is available as of Rust 1.30. Here is the complete set of files I tested with:

    Cargo.toml

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

    foo/Cargo.toml

    [package]
    name = "foo"
    version = "0.1.0"
    authors = ["An Devloper "]
    
    [dependencies]
    

    foo/src/main.rs

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

    bar/Cargo.toml

    [package]
    name = "bar"
    version = "0.1.0"
    authors = ["An Devloper "]
    
    [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 "]
    
    [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!
    

    Multiple Binaries

    As of Rust 1.37.0 you can use Cargo's "default-run" feature to specify which one to use.

    foo/Cargo.toml

    [package]
    name = "foo"
    version = "0.0.1"
    authors = ["An Devloper "]
    default-run = "foo"
    

    0 讨论(0)
提交回复
热议问题