Skip to content
Latchkey

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

Terminal
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-dependency

Common 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

FlagEffect
--features <list>Enable crate features
--devAdd to dev-dependencies
--buildAdd to build-dependencies
--optionalMark dependency optional
--no-default-featuresDisable default features

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →