cargo-add
Add dependencies to your Cargo.toml manifest
TLDR
Add the latest version of a dependency to the current project
Add a specific version of a dependency
Add a dependency and enable one or more specific features
Add an optional dependency, which then gets exposed as a feature of the crate
Add a local crate as a dependency
Add a development or build dependency
Add a dependency with all default features disabled
SYNOPSIS
cargo add [OPTIONS]
PARAMETERS
The name of the crate to add. Can optionally include a version, path, or URL after an '@' symbol (e.g., serde@1.0, my-crate@./path/to/crate, my-repo@https://github.com/user/repo).
--dev
Add the dependency to [dev-dependencies].
--build
Add the dependency to [build-dependencies].
--target
Add the dependency to [target.'
--optional
Add the dependency as an optional dependency.
--no-default-features
Do not activate the crate's default features.
--default-features
Activate the crate's default features (default).
--features
Space-separated list of features to activate for the dependency.
--path
Add a local path dependency.
--git
Add a Git repository dependency.
--branch
Branch to use for a Git dependency.
--tag
Tag to use for a Git dependency.
--rev
Specific revision to use for a Git dependency.
--version
Specify the exact version to add. Uses caret requirement by default if not specified (e.g., ^1.2.3).
--rename
Rename the dependency in Cargo.toml.
--package
Add dependency to a specific package in a workspace.
--workspace
Add dependency to the workspace root's Cargo.toml.
--dry-run
Do not actually write to Cargo.toml, just show what would happen.
--offline
Go offline and use only locally cached dependencies.
--verbose, -v
Use verbose output.
--quiet, -q
No output besides errors.
--color
Control coloring of output (auto, always, never).
--help
Print help information.
DESCRIPTION
cargo-add is a Cargo subcommand that simplifies the process of adding a dependency to your Rust project's Cargo.toml manifest file.
Instead of manually editing the file, cargo-add automatically determines the latest compatible version of the specified crate, adds the appropriate entry to the [dependencies] section (or [dev-dependencies], [build-dependencies], [target.'...'.dependencies] based on options), and updates your project's dependency graph. It supports adding regular, development, build, and target-specific dependencies, as well as dependencies from local paths or Git repositories. This command streamlines dependency management, making it quicker and less error-prone to set up new projects or introduce new libraries.
CAVEATS
When adding a dependency from crates.io without specifying a version, cargo-add will fetch the latest version. This adds a caret (^) requirement, meaning future builds might resolve to newer compatible versions, which could subtly change behavior if strict versioning is not desired.
For local path or Git dependencies, ensure the specified path or URL is correct and accessible. If no exact version is specified for crates.io dependencies, an internet connection is generally required to fetch metadata.
DEPENDENCY RESOLUTION
By default, cargo-add will attempt to find the latest compatible version of a crate from crates.io if no version is specified. It then adds a caret (^) requirement (e.g., ^1.2.3) to Cargo.toml, allowing Cargo to use newer compatible versions in future builds.
WORKSPACE SUPPORT
In a Cargo workspace, cargo-add can be used to add dependencies to a specific package within the workspace using the --package (-p) option, or to the workspace root's Cargo.toml using the --workspace flag, facilitating centralized or per-member dependency management.
FEATURES
The command allows fine-grained control over crate features. You can disable default features with --no-default-features and specify custom features with the --features option, enabling you to tailor the dependency's functionality to your project's needs.
HISTORY
Initially, cargo-add was distributed as an external subcommand, often installed via cargo install cargo-edit, providing a useful utility for Rust developers. Recognizing its widespread utility and user demand, it was integrated directly into the core Cargo toolchain. This integration became official with Rust 1.62.0, released on June 23, 2022, making it a first-class subcommand and removing the need for a separate installation step.
SEE ALSO
cargo(1), cargo-remove(1), cargo-update(1), cargo-build(1), Cargo.toml(5)