Workspace Inheritance
In a workspace with ten crates, the same facts are repeated ten times: the version (if they release together), the edition, the license, the MSRV, and "serde 1.0.200 with these features". They drift. Workspace inheritance (stable since Cargo 1.64) lets members take these values from the root.
The three inheritable tables
| Root table | Member writes | For |
|---|---|---|
[workspace.package] |
version.workspace = true |
version, edition, rust-version, license, authors, repository, homepage, documentation, readme, description, keywords, categories, publish, include, exclude… |
[workspace.dependencies] |
serde = { workspace = true } or serde.workspace = true |
dependency version, source, default features |
[workspace.lints] |
[lints] workspace = true |
rustc and Clippy lint levels |
Nothing is inherited automatically: each member opts in, key by key. That's deliberate: a member can still use its own description or a different version.
A full example
mkdir ws && cd ws
cat > Cargo.toml <<'T'
[workspace]
members = ["store", "server"]
resolver = "3"
[workspace.package]
version = "0.3.0"
edition = "2024"
license = "MIT"
rust-version = "1.85"
[workspace.dependencies]
serde = { version = "1.0.200", default-features = false }
store = { path = "store", version = "0.3.0" }
[workspace.lints.rust]
unsafe_code = "forbid"
T
mkdir -p store/src server/src
cat > store/Cargo.toml <<'T'
[package]
name = "store"
description = "A tiny key-value store"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[dependencies]
serde = { workspace = true, features = ["derive", "std"] }
[lints]
workspace = true
T
cat > store/src/lib.rs <<'RS'
#[derive(serde::Serialize)]
pub struct Entry { pub key: String, pub value: u32 }
pub fn get() -> Entry { Entry { key: "answer".into(), value: 42 } }
RS
cat > server/Cargo.toml <<'T'
[package]
name = "server"
version.workspace = true
edition.workspace = true
publish = false
[dependencies]
store.workspace = true
[lints]
workspace = true
T
echo 'fn main() { let e = store::get(); println!("{} = {} (v{})", e.key, e.value, env!("CARGO_PKG_VERSION")); }' > server/src/main.rs
cargo run -q -p server 2>&1 | grep -v Unpacking
cargo metadata --no-deps --format-version 1 > meta.json
python3 -c '
import json
for p in json.load(open("meta.json"))["packages"]:
print(p["name"], p["version"], p["edition"], p["license"], p["rust_version"])'
echo "--- the lint is inherited too"
echo 'pub fn raw() -> u8 { unsafe { *(&7u8 as *const u8) } }' >> store/src/lib.rs
cargo build -q -p store 2>&1 | grep -E '^error|forbid'answer = 42 (v0.3.0)
store 0.3.0 2024 MIT 1.85
server 0.3.0 2024 None None
--- the lint is inherited too
error: usage of an `unsafe` block
error: could not compile `store` (lib) due to 1 previous error
storeandservershare the version, edition and lint policy; onlystoretook the license and MSRV.- The workspace dependency
storedeclares bothpathandversion: members use the path, and whenserveris published the registry version is used. features = ["derive", "std"]in the member is added to what the workspace declared. Because the workspace turned serde's default features off,std(which providesSerializeforString) has to be requested explicitly; without it the derive fails with the traitSerializeis not implemented forString. Apart fromfeaturesandoptional, a member can't change an inherited dependency: the version, the source anddefault-featurescome from the workspace. So if any member needs default features off, declaredefault-features = falseat the workspace level and let the others add the features they need.
What a published crate looks like
Inheritance is a workspace concept, and a crate on a registry has no workspace. cargo package therefore writes a normalised Cargo.toml into the .crate with every inherited value filled in (and keeps your original as Cargo.toml.orig):
mkdir ws && cd ws
cat > Cargo.toml <<'T'
[workspace]
members = ["store"]
resolver = "3"
[workspace.package]
version = "0.3.0"
edition = "2024"
license = "MIT"
[workspace.dependencies]
anyhow = "1.0.100"
T
mkdir -p store/src
cat > store/Cargo.toml <<'T'
[package]
name = "store"
description = "A tiny key-value store"
documentation = "https://example.com/store"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
anyhow.workspace = true
T
echo 'pub fn ok() -> anyhow::Result<()> { Ok(()) }' > store/src/lib.rs
cd store
cargo package --no-verify -q 2>&1
tar -tzf ../target/package/store-0.3.0.crate
tar -xzOf ../target/package/store-0.3.0.crate store-0.3.0/Cargo.toml | grep -v '^#' | sed -n '/^\[package\]/,$p'store-0.3.0/Cargo.lock
store-0.3.0/Cargo.toml
store-0.3.0/Cargo.toml.orig
store-0.3.0/src/lib.rs
[package]
edition = "2024"
name = "store"
version = "0.3.0"
build = false
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "A tiny key-value store"
documentation = "https://example.com/store"
readme = false
license = "MIT"
[lib]
name = "store"
path = "src/lib.rs"
[dependencies.anyhow]
version = "1.0.100"
All the .workspace = true keys are gone, replaced by values. Cargo also writes out explicit target settings (autobins = false, [lib] path = ...) so the published crate builds the same way no matter which Cargo version reads it.
Practical advice
- Put every third-party dependency used by more than one member into
[workspace.dependencies]. One place to bump serde; no chance of two members asking for different minimums. - Put internal crates there too, with both
pathandversion, so publishing works. - Share
edition,rust-version,licenseandrepository; keepdescription,keywordsandreadmeper crate. - Use
[workspace.lints]for policy (unsafe_code = "forbid", Clippy groups) instead of#![deny(...)]at the top of everylib.rs.
Test yourself
How does a member take edition from [workspace.package]?
- Automatically
edition.workspace = trueedition = "workspace"inherit = ["edition"]
The workspace declares serde = { version = "1", default-features = false }. A member writes serde = { workspace = true, features = ["derive"] }. What does it get?
- Only derive, default features on
- Default features off, plus derive
- An error: features can't be added
- serde with all features
What happens to version.workspace = true when the crate is packaged for publishing?
- It's published as is
- cargo package replaces it with the actual value in a normalised Cargo.toml
- Publishing fails
- The registry resolves it