Rust Cargo workspace

Important Note: tests (cargo test) will not work for all crates (defined in the workspaces), if the main Cargo.toml (virtual manifest) will have specified [package] (name, version, edition). Best to keep workspace only (?)

Example of working workspace setup:

  • crates

    • lib1 (--lib) lib1/src/lib.rs

      [package]
      name = "lib1"
      version = "0.1.0"
      edition = "2021"
    • lib2 (--lib) lib2/src/lib.rs

      [package]
      name = "lib2"
      version = "0.1.0"
      edition = "2021"
  • yourpackage (--bin) yourpackage/src/main.rs Cargo.toml

    [dependencies]
    # includes your libs
    lib1 = { path = "../lib1" }
    lib2 = { path = "../lib2" }
  • Cargo.toml (virtual - specify folders for libs)

[workspace]
members = ["crates/*"]

Run tests for all packages:

cargo test # in the main folder - will run tests also for lib1, and lib2

Important Note: If you put dependencies in this Cargo.toml (the virtual setup / workspace), the 'cargo test' will not run tests in the lib1, and lib2 etc., so it seems the safest is to just put workspace (?).

Multiple binaries

if you have multiple examples (usage of cargo run --example) with the same name or binaries (usage of cargo run --bin), you may want to use 'default-members' in the workspace definition. Please see below.

Example workspace

Here contains includes

[workspace]
members = ["crates/*", "some/other/path/member2"]
exclude = ["crates/foo", "path/to/other"]
default-members = ["crates/lib1"]
If you found it valuable, please join to the nexss.com supporters at: Support Nexss.com