/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
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.tomlOutput
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
| Command | Result in Cargo.toml |
|---|---|
| cargo add tokio --features full | tokio = { version = "...", features = ["full"] } |
| cargo add serde -F derive,rc | Several features (-F is short for --features) |
| cargo add regex@1.10 | regex = "1.10" (compatible updates allowed, like ^1.10) |
| cargo add rand --no-default-features -F std | default-features = false, features = ["std"] |
| cargo add --dev pretty_assertions | Added to [dev-dependencies] |
| cargo add --build cc | Added to [build-dependencies] |
| cargo add --path ../mylib | Local path dependency |
| cargo add --git https://github.com/OWNER/REPO NAME | Git dependency |
| cargo add -p server anyhow | Add to one package of a workspace |
Remove a dependency
cargo remove serde # Cargo 1.66+
cargo remove --dev pretty_assertionsSources
Related pages
cheat sheets · cargoCargo cheat sheet: build, run, test and manage Rust cratesCargo cheat sheet: new packages, check, build and run in debug or release, test, fmt, clippy, add and update dependencies, workspaces and flags for CI.flags · cargocargo build --release: optimised builds and target/releasecargo build --release uses the release profile (opt-level 3, no overflow checks) and writes to target/release. Debug vs release, and how to tune the profile.errors · cargoerror: could not find `Cargo.toml` in `...` or any parent directorycargo reports could not find Cargo.toml when run outside a Rust package. cd into the project, pass --manifest-path, or create a package with cargo new.commands · cargocargo new: create a new Rust project (binary or library)cargo new hello creates a Rust binary package with Cargo.toml, src/main.rs and Git; --lib makes a library. Options, naming rules, cargo init and pitfalls.