Field Notes: Dependencies & Features
Habits from teams that maintain Rust crates and large workspaces. Each one prevents a specific, recurring kind of breakage.
1. Features must be additive, and you must test them that way
Unification (lesson) means someone else's dependency can turn your feature on. So:
- A feature adds API or capability. It never removes items, changes return types, changes the meaning of existing functions or excludes another feature.
- "Choose one" belongs in code (a type parameter, a constructor, a runtime option), not in features.
no_stdsupport is spelledstd = [](a default feature that adds std-only API), neverno_std = [].- Every feature should compile on its own:
cargo hack check --each-featurein CI. The usual bug is a feature that only builds becausedefaulthappened to pull in something it needs.
2. In libraries, turn default features off and ask for what you use
Each dependency's default features are often much more than a library needs (std, formatting, Unicode tables, async runtimes). Libraries should depend with default-features = false and list the features they use, so applications keep control over the final feature set. Unification means you can never take a feature away from a crate, only add.
[dependencies]
regex = { version = "1.12", default-features = false, features = ["std", "unicode-perl"] }3. Requirements: precise minimums, never *
- Write the version you actually tested as the minimum:
serde = "1.0.200", not"1". If you use an API added in 1.0.200,"1"is a lie that breaks users who are locked to 1.0.150. - Never
*. crates.io rejects wildcard requirements at publish time, and in a private registry it guarantees an unreviewed breaking upgrade one day. - Avoid
=and upper bounds (<1.5) in libraries: they make the graph unsolvable for users when another crate needs a newer version. They're acceptable in applications, temporarily, with a comment explaining why. - Nightly's
cargo update -Z minimal-versionsresolves every requirement to its lowest allowed version; running tests that way occasionally proves your minimums are honest.
4. Re-export what's in your public API
If a type from crate X appears in your public functions, add pub use x; (or re-export the specific types). Users then name exactly the version you compiled against, and the "expected trait Rng, found trait Rng" errors from Duplicate Versions go away. Bumping that dependency's major version is then a breaking change of your crate, so treat it as one.
5. Let the resolver respect your MSRV
Set rust-version in every package. With resolver "3" (the default for edition 2024), Cargo prefers dependency versions whose own rust-version is compatible with yours, instead of silently picking a release that needs a newer compiler:
crate-registry start
mkdir -p units/src && cd units
release() {
cat > Cargo.toml <<T
[package]
name = "units"
version = "$1"
edition = "2021"
rust-version = "$2"
description = "Unit helpers"
license = "MIT"
documentation = "https://example.com/units"
T
echo "pub const V: &str = \"$1\";" > src/lib.rs
cargo publish --registry local --no-verify -q
}
release 1.0.0 1.80
release 1.1.0 1.98
cd ..
cargo new app --vcs none -q
cd app
echo 'units = { version = "1", registry = "local" }' >> Cargo.toml
echo 'fn main() { println!("units {}", units::V); }' > src/main.rs
echo "--- no rust-version: newest wins"
cargo generate-lockfile 2>&1 | grep -v Updating
cargo run -q
echo "--- rust-version = 1.85"
sed -i 's/^edition.*/&\nrust-version = "1.85"/' Cargo.toml
rm Cargo.lock
cargo generate-lockfile 2>&1 | grep -v Updating
cargo run -q--- no rust-version: newest wins
Locking 1 package to highest Rust 1.99.1 compatible version
units 1.1.0
--- rust-version = 1.85
Locking 1 package to highest Rust 1.85 compatible version
Adding units v1.0.0 (registry `local`) (available: v1.1.0, requires Rust 1.98)
units 1.0.0
Without an MSRV, Cargo took 1.1.0, which needs Rust 1.98, and anyone on an older compiler would get a build error. With rust-version = "1.85" it chose 1.0.0 and said why 1.1.0 was skipped. It's a preference, not a hard rule: if no compatible version exists, Cargo still picks the newest one.
6. Watch the graph, not just the manifest
Most dependency problems live in the transitive graph, which nobody reviews in pull requests. Make it visible:
cargo tree -din CI (or cargo-deny'smultiple-versions), so new duplicates are a conscious choice.- Review
Cargo.lockdiffs: a one-lineCargo.tomlchange that adds 40 packages deserves a question. - cargo-deny for licenses and sources, cargo-audit or
cargo deny check advisoriesfor known vulnerabilities, on a schedule, not just on pull requests (advisories appear for code that hasn't changed). - Dependabot or Renovate for regular, small, reviewed updates rather than a yearly "update everything" pull request.
7. Path + version for internal crates
Inside a workspace, depend on siblings with both path and version (ideally via [workspace.dependencies]). Path-only dependencies work until the day you publish, and cargo publish then refuses: the registry has no idea where ../core is.
Test yourself
A library needs only regex's std and unicode-perl features. How should it declare the dependency?
- regex = "1"
- regex = { version = "1.12", default-features = false, features = ["std", "unicode-perl"] }
- regex = { version = "*", features = ["std"] }
- regex = "=1.12.0"
With rust-version = "1.85" and resolver 3, units 1.1.0 needs Rust 1.98 and 1.0.0 needs 1.80. Which does cargo generate-lockfile pick for units = "1"?
- 1.1.0, the newest
- 1.0.0, the newest compatible with the declared MSRV
- It fails
- Both
Why is no_std = [] a bad feature name/design?
- Feature names can't contain underscores
- Enabling it removes functionality, which breaks additivity when another crate enables it
- It's reserved by Cargo
- It slows the build