Skip to content

/commands/cargo/cargo-add-dependency-features

cargo add serde --features derive: add a dependency with features

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · cargo 1.98.1 (797e8a9bc 2026-08-05)updated
tool
Cargo
versions
Cargo 1.62+ (cargo add built in), cargo remove: Cargo 1.66+
os
any

cargo add serde --features derive adds serde to the [dependencies] table of Cargo.toml with the derive feature enabled, choosing the latest compatible version from crates.io. It is built into Cargo since 1.62 (before that it came from the cargo-edit plugin). Add @VERSION to pin a version, and --dev or --build for other dependency tables.

Add a crate with a feature

bash
cargo new -q --vcs none app && cd app
cargo add serde@1.0.219 --features derive 2>&1 | grep -vE 'Updating|Locking'
echo "--- Cargo.toml"
sed -n '/^\[dependencies\]/,$p' Cargo.toml

Output

      Adding serde v1.0.219 to dependencies
             Features:
             + derive
             + serde_derive
             + std
             - alloc
             - rc
             - unstable
--- Cargo.toml
[dependencies]
serde = { version = "1.0.219", features = ["derive"] }

cargo add prints the crate's features: + marks features that will be enabled (the ones you asked for, the defaults, and features they turn on), - marks available features that stay off. Without @1.0.219, Cargo writes the newest version, such as serde = { version = "1.0.N", ... }.

Options

CommandResult in Cargo.toml
cargo add tokio --features fulltokio = { version = "...", features = ["full"] }
cargo add serde -F derive,rcSeveral features (-F is short for --features)
cargo add regex@1.10regex = "1.10" (compatible updates allowed, like ^1.10)
cargo add rand --no-default-features -F stddefault-features = false, features = ["std"]
cargo add --dev pretty_assertionsAdded to [dev-dependencies]
cargo add --build ccAdded to [build-dependencies]
cargo add --path ../mylibLocal path dependency
cargo add --git https://github.com/OWNER/REPO NAMEGit dependency
cargo add -p server anyhowAdd to one package of a workspace

Remove a dependency

bash
cargo remove serde          # Cargo 1.66+
cargo remove --dev pretty_assertions

Sources