CARGO-ADD(1)
πΈ CARGO-ADD(1) πΈ
okay bestie, welcome to cargo-add(1) β it's a General Commands Manual page and it's literally about adding deps to your Cargo.toml without you having to hand-edit TOML like it's 2015. no cap, this one's a glow-up. π«Ά
β¨ NAME β¨
cargo-add β Add dependencies to a Cargo.toml manifest file π
β¨ SYNOPSIS β¨
cargo add [options] crateβ¦
cargo add [options] --path path
cargo add [options] --git url [crateβ¦]
β¨ DESCRIPTION β¨
This command can add or modify dependencies. that's the whole job. it's giving "personal assistant but for your manifest". π
The source for the dependency can be specified 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 at url
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. so it's not a duplicate situation, it's a makeover situation. β¨
Upon successful invocation, the enabled (+) and disabled (-) features <https://doc.rust-lang.org/cargo/reference/features.html> of the specified dependency will be listed in the command's output. so you literally get receipts on which features are on and off. obsessed. π§Ύ
β¨ 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>. pulling straight from a repo, main character behavior.--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. pinning it DOWN fr fr.--path_path_ β Filesystem path <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-path-dependencies> to local crate to add.--base_base_ β The path base <https://doc.rust-lang.org/cargo/reference/unstable.html#path-bases> to use when adding a local crate.
Unstable (nightly-only) <https://doc.rust-lang.org/cargo/reference/unstable.html#path-bases>
--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)'. yes bestie, QUOTES. your shell will absolutely jump you otherwise. π
π Dependency options
--dry-runβ Don't actually write the manifest. window shopping only, no commitment. ποΈ--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. stack them features, bestie, it's a whole vibe stack. π₯
π 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. bestie it's giving "we do NOT do surprises here". π«
--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.--lockfile-path_PATH_ β Changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) to _PATH_. _PATH_ must end withCargo.lock(e.g.--lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that providing--lockfile-pathwill ignore existing lockfile at the default path, and instead will either use the lockfile from _PATH_, or write a new lockfile into the provided _PATH_ if it doesn't exist. This flag can be used to run most commands in read-only directories, writing lockfile into the provided _PATH_.
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 #14421 <https://github.com/rust-lang/cargo/issues/14421>).
π 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=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-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.
β¨ EXIT STATUS β¨
0: Cargo succeeded. π slay.101: Cargo failed to complete. π it happens to the best of us.
β¨ 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 β¨
cargo(1), cargo-remove(1) π¦
okay that's the whole page, bestie, and just so you have the receipts: this was CARGO-ADD(1) from the General Commands Manual, and the footer at the very bottom said CARGO-ADD(1) too β no version or date line was hanging out down there, so nothing to report on that front. go add some crates, you're doing amazing, mwah. πβ¨π
π¦ where this page comes from
- ubuntu jammy: cargo-1.84 1.84.1+dfsg0ubuntu1~bpo10-0ubuntu4.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.84 1.84.1+dfsg0ubuntu1~bpo2-0ubuntu2.24.04.1 Β· Apache-2.0 WITH LLVM exception
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms