cargo add: Usage, Options & Common CI Errors
Add a dependency to Cargo.toml from the command line.
cargo add inserts a dependency into Cargo.toml, picking a compatible version requirement and optionally enabling features. It replaced hand-editing the manifest for most workflows.
What it does
Resolves the latest compatible version of a crate, writes a dependency entry into the appropriate table (dependencies, dev-dependencies, or build-dependencies), and can enable features in the same call.
Common usage
cargo add serde # latest compatible
cargo add serde --features derive # enable a feature
cargo add tokio@1.38 --features full
cargo add --dev assert_cmd # dev-dependency
cargo add --build cc # build-dependencyCommon CI error: unknown feature
cargo add foo --features bar fails with "error: unrecognized feature for crate foo: bar" when the feature name is wrong or gated behind another feature. Fix: run cargo add foo with no --features to see the available list it prints, then add the correct one.
Options
| Flag | Effect |
|---|---|
| --features <list> | Enable crate features |
| --dev | Add to dev-dependencies |
| --build | Add to build-dependencies |
| --optional | Mark dependency optional |
| --no-default-features | Disable default features |