CARGO-ADD(1)
okay bestie, welcome to the cargo-add(1) page from the General Commands Manual β it's literally here to help you add deps to a Cargo.toml manifest file, and it ate π
β¨ NAME β¨
cargo-add β add dependencies to a Cargo.toml manifest file. no cap, that's the whole point π«Ά
π SYNOPSIS π
the vibes in command form:
cargo add [options] crateβ¦
cargo add [options] --path path
cargo add [options] --git url [crateβ¦]
πΈ DESCRIPTION πΈ
this command can add or modify dependencies. yes, both. it's giving multitasking queen π
the source for the dependency can be specified with:
crate@version: fetch from a registry with a version constraint ofversion--path path: fetch from the specified path--git url: pull from a git repo aturl
if no source is specified, then a best effort will be made to select one, including:
- existing dependencies in other tables (like
dev-dependencies) - workspace members
- latest release in the registry
when you add a package that is already present, the existing entry will be updated with the flags specified. periodt.
upon successful invocation, the enabled (+) and disabled (-) features (the whole https://doc.rust-lang.org/cargo/reference/features.html situation) of the specified dependency will be listed in the commandβs output. iconic behavior, honestly β¨
π OPTIONS π
okay here's the full menu, bestie. every flag, every value, no skips π
π 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 (https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-path-dependencies) to local crate to add.--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 not specified, the default registry is used, which is defined by theregistry.defaultconfig key which 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)'.
- To avoid unexpected shell expansions, you may use quotes around each target, e.g.,
π Dependency options π
--dry-runβ Donβt actually write the manifest.--rename nameβ Rename (https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml) the dependency.--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 adding multiple crates, the features for a specific crate may be enabled withpackage-name/feature-namesyntax. This flag may be specified multiple times, which enables all specified features.
β¨ 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.colorconfig 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.
--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 thecargo-fetch(1)command to download dependencies before going offline. May also be specified with thenet.offlineconfig value (https://doc.rust-lang.org/cargo/reference/config.html).--frozenβ Equivalent to specifying both--lockedand--offline.
π Common Options π
+toolchainβ If Cargo has been installed with rustup, and the first argument to cargo begins 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=VALUE or PATHβ 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-optionsflag to enable (see#10098https://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. iykyk π
π 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 π―
that's the whole CARGO-ADD(1) page from the General Commands Manual, bestie β go add those deps and slay β¨π
π¦ where this page comes from
- ubuntu jammy: cargo-1.80 1.80.1+dfsg0ubuntu1~bpo0-0ubuntu0.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.80 1.80.1+dfsg0ubuntu1-0ubuntu0.24.04.01 Β· MIT OR Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms