Workspaces: Members, -p & --workspace
A workspace is a set of packages that are developed together and share three things: one Cargo.lock, one target/ directory and one set of resolved versions. Almost every Rust project bigger than a single crate is a workspace: a library plus a CLI, a server split into crates for faster compiles, a family of crates published together.
Creating one
The root Cargo.toml has a [workspace] table. If it has no [package], it's a virtual workspace (the most common layout). cargo new inside a workspace registers the new package as a member automatically:
mkdir shop && cd shop
printf '[workspace]\nresolver = "3"\n' > Cargo.toml
cargo new --lib pricing --vcs none
cargo new api --vcs none -q
cargo new admin --vcs none -q
cat Cargo.toml
cat > pricing/src/lib.rs <<'RS'
pub fn with_vat(cents: u64) -> u64 { cents * 120 / 100 }
#[cfg(test)]
mod tests {
#[test]
fn adds_twenty_percent() { assert_eq!(super::with_vat(1000), 1200); }
}
RS
for p in api admin; do
echo 'pricing = { path = "../pricing" }' >> $p/Cargo.toml
echo 'fn main() { println!("{}: 10.00 -> {}", env!("CARGO_PKG_NAME"), pricing::with_vat(1000)); }' > $p/src/main.rs
done
cargo build 2>&1
ls
ls api Creating library `pricing` package
Adding `pricing` as member of workspace at `/home/learner/shop`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[workspace]
resolver = "3"
members = ["admin","api","pricing"]
Compiling pricing v0.1.0 (/home/learner/shop/pricing)
Compiling api v0.1.0 (/home/learner/shop/api)
Compiling admin v0.1.0 (/home/learner/shop/admin)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.30s
Cargo.lock
Cargo.toml
admin
api
pricing
target
Cargo.toml
src
Compiling pricing appears once even though two binaries use it, and there is one Cargo.lock and one target/ at the root; api/ has neither. The members list was written by cargo new (globs like members = ["crates/*"] also work).
Choosing what to build: -p, --workspace, --exclude
| Where you run it / flags | What Cargo builds |
|---|---|
| in a member's folder, no flags | that member |
| at the root of a virtual workspace, no flags | default-members if set, otherwise all members |
-p api (--package), repeatable |
the named members |
--workspace |
all members, from anywhere |
--workspace --exclude admin |
all except some |
mkdir shop && cd shop
cargo new --lib pricing --vcs none -q
cargo new api --vcs none -q
cargo new admin --vcs none -q
printf '[workspace]\nresolver = "3"\nmembers = ["pricing", "api", "admin"]\ndefault-members = ["api"]\n' > Cargo.toml
echo 'pub fn with_vat(cents: u64) -> u64 { cents * 120 / 100 }' > pricing/src/lib.rs
for p in api admin; do
echo 'pricing = { path = "../pricing" }' >> $p/Cargo.toml
echo 'fn main() { println!("{}: {}", env!("CARGO_PKG_NAME"), pricing::with_vat(1000)); }' > $p/src/main.rs
done
echo "--- cargo run at the root (default-members = api)"
cargo run -q
echo "--- -p admin"
cargo run -q -p admin
echo "--- inside admin/"
(cd admin && cargo run -q)
echo "--- check everything except admin"
cargo check --workspace --exclude admin 2>&1--- cargo run at the root (default-members = api)
api: 1200
--- -p admin
admin: 1200
--- inside admin/
admin: 1200
--- check everything except admin
Checking pricing v0.1.0 (/home/learner/shop/pricing)
Checking api v0.1.0 (/home/learner/shop/api)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.10s
default-members is useful when a workspace has slow or platform-specific members that most developers don't need on every build. CI should still use --workspace.
Tests and the shared lockfile
cargo test --workspace runs every member's tests in one command; cargo test -p pricing just one. Because the lockfile is shared, every member uses the same version of each dependency (within SemVer compatibility, as in Duplicate Versions): if api and admin both depend on serde = "1", they get the same serde, compiled once. This is the main reason to group related packages into a workspace rather than keeping them as separate projects that point at each other with path.
What lives only at the root
Some settings are ignored in member manifests and must be in the root Cargo.toml:
[profile.*]: one build has one set of profiles. Cargo warns: profiles for the non root package will be ignored.[patch]and[replace].resolver: a virtual workspace must set it explicitly, because it has no edition to derive it from.
Layouts that work
shop/
├── Cargo.toml [workspace] + shared settings, no [package]
├── Cargo.lock
├── crates/
│ ├── pricing/ library
│ ├── storage/ library
│ └── api/ binary
└── xtask/ build automation written in Rust (cargo xtask pattern)A flat crates/* folder with members = ["crates/*"] scales well; rust-analyzer, Cargo and CI tools all handle it naturally. Packages that must not be members (a fuzzing harness, an example with its own lockfile) go in exclude = ["fuzz"].
Test yourself
Two members of one workspace both depend on pricing. How many times is pricing compiled by cargo build --workspace?
- Once per member
- Once: the workspace shares one target directory and lockfile
- Twice: debug and release
- It depends on the member order
You run cargo build with no flags at the root of a virtual workspace without default-members. What is built?
- Nothing; it needs -p
- All members
- Only the first member
- Only libraries
Where must [profile.release] go in a workspace?
- In each member
- In the root Cargo.toml
- In .cargo/config.toml only
- In the binary member