CARGO-ADD(1)
β¨ CARGO-ADD(1) β¨
okay so bestie, welcome to the cargo-add manual page β it's a general commands manual page, and it's genuinely one of the most useful little gremlins in the whole cargo toolbox π
πΈ NAME πΈ
cargo-add β add dependencies to a Cargo.toml manifest file. that's it. that's the job. and it ATE π
π SYNOPSIS π
cargo add [options] crateβ¦
cargo add [options] --path path
cargo add [options] --git url [crateβ¦]
three whole moods. pick your fighter β¨
π¦ DESCRIPTION π¦
so like, this command can add or modify dependencies. lowkey it does both, highkey it does both.
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 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. no dupe drama, it just updates π«Ά
upon successful invocation, the enabled (+) and disabled (-) features of the specified dependency will be listed in the command's output. the docs for that are over at https://doc.rust-lang.org/cargo/reference/features.html β go read them, i'll wait π
β¨ OPTIONS β¨
π« Source options π«
--git urlβ the git URL to add the specified crate from. details athttps://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositoriesπ§--branch branchβ which branch to use when adding from git.--tag tagβ which tag to use when adding from git.--rev shaβ the specific commit to use when adding from git.--path pathβ filesystem path to the local crate you wanna add. seehttps://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-path-dependencies--base baseβ the path base to use when adding a local crate. the docs live athttps://doc.rust-lang.org/cargo/reference/unstable.html#path-bases
unstable, nightly-only bestie π https://doc.rust-lang.org/cargo/reference/unstable.html#path-bases
--registry registryβ the 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, 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)'. quoting is your friend, not me being dramatic π
π Dependency options π
--dry-runβ don't actually write the manifest. it's giving window shopping π--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. same link: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.
also unstable, also 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-featureswhen 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. spam it, it's fine π
πΈ 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. it's a little detective π-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. no cap, very serious girlboss behaviour π
--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 deal, slay.--lockfile-path PATHβ changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) toPATH.PATHmust 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 fromPATH, or write a new lockfile into the providedPATHif it doesn't exist. this flag can be used to run most commands in read-only directories, writing lockfile into the providedPATH.
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 #14421https://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 athttps://rust-lang.github.io/rustup/overrides.htmlfor 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 athttps://doc.rust-lang.org/cargo/reference/config.html#command-line-overridesfor 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. bestie behaviour fr fr π«Ά-Z flagβ unstable (nightly-only) flags to Cargo. runcargo -Z helpfor details.
π ENVIRONMENT π
see the reference at https://doc.rust-lang.org/cargo/reference/environment-variables.html for details on environment variables that Cargo reads. i'm not typing them all out here, the reference ate π
β 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 β¨ this was the CARGO-ADD(1) page from the general commands manual, filed under CARGO-ADD(1) in the footer β go forth and add dependencies responsibly, ily πππ¦
π¦ where this page comes from
- ubuntu questing: cargo-1.88 1.88.0+dfsg0ubuntu1-0ubuntu1
- ubuntu questing: cargo-1.88 1.88.0+dfsg0ubuntu1-0ubuntu2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms