CARGO-ADD(1)
π NAME π
cargo-add β the command that adds dependencies to a Cargo.toml manifest file. Manual name's cargo-add(1), straight out of the General Commands Manual, and she is serving.
π¦ SYNOPSIS π¦
cargo add [options] crateβ¦
cargo add [options] --path path
cargo add [options] --git url [crateβ¦]
β¨ DESCRIPTION β¨
So basically, this command can add or modify dependencies. That's the whole job, and she does it flawlessly.
Where the dependency comes from? You can tell her with:
crate@version: fetch from a registry with a version constraint of βversionβ--path path: fetch from the specified path--git url: pull from a git repo aturl
If you don't specify a source, she'll do a best-effort lil detective moment to pick one for you, including:
- Existing dependencies in other tables (like
dev-dependencies) - Workspace members
- Latest release in the registry
When you add a package that's already there? No drama β the existing entry gets updated with the flags you specified. Not a duplicate, just a glow-up. π
Upon successful invocation, the enabled (+) and disabled (-) features of the specified dependency get listed right there in the command's output. Features docs live at <https://doc.rust-lang.org/cargo/reference/features.html> if you wanna read ahead, bestie.
π OPTIONS π
π Source options
--git urlβ Git URL to add the specified crate from. <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories>--branch branchβ Branch to use when adding from git.--tag tagβ Tag to use when adding from git.--rev shaβ Specific commit to use when adding from git.--path pathβ Filesystem path to the local crate to add. <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-path-dependencies>--base baseβ The path base to use when adding a local crate. <https://doc.rust-lang.org/cargo/reference/unstable.html#path-bases>
Unstable (nightly-only) <https://doc.rust-lang.org/cargo/reference/unstable.html#path-bases> β yeah she's not out of beta yet, sorry babe.
--registry registryβ Name of the registry to use. Registry names are defined in Cargo config files <https://doc.rust-lang.org/cargo/reference/config.html>. If you don't specify one, the default registry is used, which is defined by theregistry.defaultconfig key, and that defaults tocrates-io.
π Section options
--devβ Add as a development dependency. <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies>--buildβ Add as a build dependency. <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#build-dependencies>--target targetβ Add as a dependency to the given target platform. <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies>
To avoid unexpected shell expansions, you may use quotes around each target, e.g., --target 'cfg(unix)'. Quote it, bestie, your shell is nosy.
π§ Dependency options
--dry-runβ Don't actually write the manifest. Just vibes. Just a preview.--rename nameβ Rename the dependency. <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml>--optionalβ Mark the dependency as optional. <https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies>--no-optionalβ Mark the dependency as required. <https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies>--publicβ Mark the dependency as public. The dependency can be referenced in your library's public API.
Unstable (nightly-only) <https://doc.rust-lang.org/cargo/reference/unstable.html#public-dependency>
--no-publicβ Mark the dependency as private. While you can use the crate in your implementation, it cannot be referenced in your public API.
Unstable (nightly-only) <https://doc.rust-lang.org/cargo/reference/unstable.html#public-dependency>
--no-default-featuresβ Disable the default features. <https://doc.rust-lang.org/cargo/reference/features.html#dependency-features>--default-featuresβ Re-enable the default features. <https://doc.rust-lang.org/cargo/reference/features.html#dependency-features>-F features,--features featuresβ Space or comma separated list of features to activate. <https://doc.rust-lang.org/cargo/reference/features.html#dependency-features> When you're adding multiple crates, the features for one specific crate may be enabled withpackage-name/feature-namesyntax. This flag may be specified multiple times, and specifying it more than once enables all the features you named. Iconic behavior honestly.
π Display Options
-v,--verboseβ Use verbose output. May be specified twice for βvery verboseβ output, which includes extra output such as dependency warnings and build script output. May also be specified with theterm.verboseconfig value. <https://doc.rust-lang.org/cargo/reference/config.html>-q,--quietβ Do not print cargo log messages. May also be specified with theterm.quietconfig value. <https://doc.rust-lang.org/cargo/reference/config.html>--color whenβ Control when colored output is used. Valid values:auto(default): Automatically detect if color support is available on the terminal.always: Always display colors.never: Never display colors.
May also be specified with the term.color config value. <https://doc.rust-lang.org/cargo/reference/config.html>
π Manifest Options
--manifest-path pathβ Path to theCargo.tomlfile. By default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory.-p spec,--package specβ Add dependencies to only the specified package.--ignore-rust-versionβ Ignorerust-versionspecification in packages.--lockedβ Asserts that the exact same dependencies and versions are used as when the existingCargo.lockfile was originally generated. Cargo will exit with an error when either of the following scenarios arises:- The lock file is missing.
- Cargo attempted to change the lock file due to a different dependency resolution.
It may be used in environments where deterministic builds are desired, such as in CI pipelines. She's the "no surprises" queen. π
--offlineβ Prevents Cargo from accessing the network for any reason. Without this flag, Cargo will stop with an error if it needs to access the network and the network is not available. With this flag, Cargo will attempt to proceed without the network if possible.
Beware that this may result in different dependency resolution than online mode. Cargo will restrict itself to crates that are downloaded locally, even if there might be a newer version as indicated in the local copy of the index. See the cargo-fetch(1) command to download dependencies before going offline.
May also be specified with the net.offline config value. <https://doc.rust-lang.org/cargo/reference/config.html>
--frozenβ Equivalent to specifying both--lockedand--offline. Two-for-one special. π
πΈ Common Options
+toolchainβ If Cargo has been installed with rustup, and the first argument tocargobegins with+, it will be interpreted as a rustup toolchain name (such as+stableor+nightly). See the rustup documentation <https://rust-lang.github.io/rustup/overrides.html> for more information about how toolchain overrides work.--config KEY=VALUEorPATHβ Overrides a Cargo configuration value. The argument should be in TOML syntax ofKEY=VALUE, or provided as a path to an extra configuration file. This flag may be specified multiple times. See the command-line overrides section <https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides> for more information.-C PATHβ Changes the current working directory before executing any specified operations. This affects things like where cargo looks by default for the project manifest (Cargo.toml), as well as the directories searched for discovering.cargo/config.toml, for example. This option must appear before the command name, for examplecargo -C path/to/my-project build.
This option is only available on the nightly channel <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and requires the -Z unstable-options flag to enable (see #10098 <https://github.com/rust-lang/cargo/issues/10098>).
-h,--helpβ Prints help information.-Z flagβ Unstable (nightly-only) flags to Cargo. Runcargo -Z helpfor details.
π± ENVIRONMENT π±
See the reference <https://doc.rust-lang.org/cargo/reference/environment-variables.html> for details on environment variables that Cargo reads. That's the whole section, she's a woman of few words when it comes to env vars. π
β EXIT STATUS β
0: Cargo succeeded. π101: Cargo failed to complete. π
π₯ EXAMPLES π₯
- Add
regexas a dependency
cargo add regex
- Add
trybuildas a dev-dependency
cargo add --dev trybuild
- Add an older version of
nomas a dependency
cargo add nom@5
- Add support for serializing data structures to json with derives
cargo add serde serde_json -F serde/derive
- Add
windowsas a platform specific dependency oncfg(windows)
cargo add windows --target 'cfg(windows)'
π«Ά SEE ALSO π«Ά
and that's the whole page bestie, no cap, every last flag accounted for π this was the CARGO-ADD(1) page from the General Commands Manual β that's the footer name right there, so now you know exactly where she lives. go forth and cargo add responsibly. xoxo π
β¨π
π¦ where this page comes from
- alpine 3.24: cargo-doc 1.96.1-r0 Β· Apache-2.0 OR MIT
- alpine 3.24: chromium-cargo-doc 1.97.0-r0 Β· Apache-2.0 OR MIT
- centos-stream 10: cargo 1.99.1-1.el10 Β· (Apache-2.0 OR MIT) AND (Artistic-2.0 AND BSD-3-Clause AND ISC AND MIT AND MPL-2.0 AND Unicode-3.0)
- centos-stream 9: cargo 1.99.1-1.el9 Β· (Apache-2.0 OR MIT) AND (Artistic-2.0 AND BSD-3-Clause AND ISC AND MIT AND MPL-2.0 AND Unicode-3.0)
- debian bookworm: cargo-web 1.96.0+dfsg1-1~deb12u2 Β· MIT OR Apache-2.0
- debian trixie: cargo-web 1.96.0+dfsg1-1~deb13u1+b1 Β· MIT OR Apache-2.0
- fedora 43: cargo 1.98.1-1.fc43
- fedora 43: cargo 1.99.0-1.fc43
- fedora 43: cargo 1.99.1-2.fc43 Β· (Apache-2.0 OR MIT) AND (Artistic-2.0 AND BSD-3-Clause AND ISC AND MIT AND MPL-2.0 AND Unicode-3.0)
- fedora 44: cargo 1.94.1-1.fc44 Β· (Apache-2.0 OR MIT) AND (Artistic-2.0 AND BSD-3-Clause AND ISC AND MIT AND MPL-2.0 AND Unicode-3.0)
- fedora 44: cargo 1.98.1-1.fc44
- fedora 44: cargo 1.99.0-1.fc44
- fedora 44: cargo 1.99.1-2.fc44 Β· (Apache-2.0 OR MIT) AND (Artistic-2.0 AND BSD-3-Clause AND ISC AND MIT AND MPL-2.0 AND Unicode-3.0)
- nixos 26.05: cargo 1.95.0
- opensuse-leap 16.0: cargo1.94 1.94.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.94 1.94.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.94 1.94.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.94 1.94.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.95 1.95.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.95 1.95.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.95 1.95.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.95 1.95.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.96 1.96.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.96 1.96.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.96 1.96.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.96 1.96.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.97 1.97.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.97 1.97.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.97 1.97.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.97 1.97.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.98 1.98.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.98 1.98.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.98 1.98.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.98 1.98.0-160000.1.1 Β· Apache-2.0 OR MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms