CARGO-ADD(1)

πŸ“¦ version 1.82.0, in ubuntu jammy, ubuntu noble

πŸ‘‘ CARGO-ADD πŸ‘‘

okay so bestie, welcome to the General Commands Manual entry for cargo-add β€” yes it's a real manual page, yes it's for a real command, and yes we're gonna get through all of it together. grab a strawberry πŸ“ and let's go.


✨ 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 πŸ¦‹

so this command adds or modifies dependencies. that's the whole vibe. it's giving "manifest glow-up". ✨

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 you don't specify a source, cargo does a best effort 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 present, the existing entry gets updated with the flags you specified. no dupe, just a rewrite ✍️

upon successful invocation, the enabled (+) and disabled (-) features of the specified dependency will be listed in the command's output. (features docs: https://doc.rust-lang.org/cargo/reference/features.html) πŸ‘‘


πŸ’– OPTIONS πŸ’–

πŸŽ€ Source options πŸŽ€

🌈 Section options 🌈

to avoid unexpected shell expansions, you may use quotes around each target, e.g. --target 'cfg(unix)'. 🐚 quote it, bestie, the shell is not your friend here.

🧚 Dependency options 🧚

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

πŸ’… 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 the term.verbose config value. (https://doc.rust-lang.org/cargo/reference/config.html)
  • -q, --quiet β€” do not print cargo log messages. may also be specified with the term.quiet config 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 the Cargo.toml file. by default, Cargo searches for the Cargo.toml file in the current directory or any parent directory.
  • -p spec, --package spec β€” add dependencies to only the specified package.
  • --ignore-rust-version β€” ignore rust-version specification in packages. πŸ™ˆ
  • --locked β€” asserts that the exact same dependencies and versions are used as when the existing Cargo.lock file 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 β€” 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 --locked and --offline. 🧊
  • --lockfile-path PATH β€” changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock (e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). note that providing --lockfile-path will 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.
Unstable β€” 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 to cargo begins with +, it will be interpreted as a rustup toolchain name (such as +stable or +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 of KEY=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 example cargo -C path/to/my-project build.
Unstable β€” 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. run cargo -Z help for 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. 😭

πŸ“ EXAMPLES πŸ“

  1. Add regex as a dependency
cargo add regex
  1. Add trybuild as a dev-dependency
cargo add --dev trybuild
  1. Add an older version of nom as a dependency
cargo add nom@5
  1. Add support for serializing data structures to json with derives
cargo add serde serde_json -F serde/derive
  1. Add windows as a platform specific dependency on cfg(windows)
cargo add windows --target 'cfg(windows)'

πŸ”₯ SEE ALSO πŸ”₯

cargo(1), cargo-remove(1)


and that's the whole page, bestie 🫢 you ate that. this one's signed off as CARGO-ADD(1) in the footer β€” no date, no extra sauce, just the name, iconic and minimal πŸ’… go add some dependencies.

πŸ“¦ where this page comes from