LinuxCommandLibrary

cargo-add

Add dependencies to your Cargo.toml manifest

TLDR

Add the latest version of a dependency to the current project

$ cargo add [dependency]
copy

Add a specific version of a dependency
$ cargo add [dependency]@[version]
copy

Add a dependency and enable one or more specific features
$ cargo add [dependency] [[-F|--features]] [feature_1,feature_2,...]
copy

Add an optional dependency, which then gets exposed as a feature of the crate
$ cargo add [dependency] --optional
copy

Add a local crate as a dependency
$ cargo add --path [path/to/crate_directory]
copy

Add a development or build dependency
$ cargo add [dependency] --[dev|build]
copy

Add a dependency with all default features disabled
$ cargo add [dependency] --no-default-features
copy

SYNOPSIS

cargo add [OPTIONS] <CRATE> […]

PARAMETERS

-p SPEC, --package SPEC
    Package in workspace to modify

--manifest-path PATH
    Path to Cargo.toml

--build
    Add as build-dependency

--dev
    Add as dev-dependency

--optional
    Mark as optional dependency

--features FEATURES
    Space/comma-separated features to activate

--no-default-features
    Disable default features

--git URL
    Git repository URL

--branch BRANCH
    Git branch

--tag TAG
    Git tag

--rev SHA
    Git revision

--path PATH
    Local path

--registry REGISTRY
    Registry index

--vers VERSION
    Version constraint (e.g., '=1.0.0')

--dry-run
    Preview changes without applying

-q, --quiet
    No output

-v, --verbose
    Verbose output

--frozen, --locked
    Don't update lockfile

DESCRIPTION

The cargo add command is a subcommand of Cargo, Rust's build tool and package manager, designed to simplify adding dependencies to a project's Cargo.toml manifest file. Rather than manually editing the file—which risks syntax errors, incorrect version constraints, or missing features—cargo add automates the process. It fetches crate metadata from registries like crates.io, resolves compatible versions, and updates the appropriate section: [dependencies], [dev-dependencies], or [build-dependencies].

Specify crates by name, optionally with version requirements (e.g., tokio = "1.0"), features, git repositories, local paths, or branches/tags. Multiple crates can be added at once. It respects Cargo's workspace structure, targeting specific packages with -p. Dry-run mode previews changes without modifying files. This streamlines workflows, ensures lockfile compatibility, and reduces boilerplate, making Rust project setup faster and less error-prone.

CAVEATS

Directly modifies Cargo.toml; backup or use version control to avoid conflicts. Version selection uses semver compatible with existing deps.

EXAMPLES

cargo add tokio serde
cargo add clap --features derive --dev
cargo add --git https://github.com/rust-lang/cargo

VERSION RESOLUTION

Defaults to caret requirements (^1.0); uses '--vers' for exact/patch pins. Respects workspace inheritance.

HISTORY

Stabilized in Cargo 1.63.0 (2022); originated as cargo-edit third-party tool by passcod/killercup since 2015.

SEE ALSO

cargo(1), cargo-edit(1)

Copied to clipboard