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 at url

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

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 the registry.default config key, and that defaults to crates-io.

πŸ“ Section options

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

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. 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 --locked and --offline. Two-for-one special. πŸ’…

🌸 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.
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. 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 πŸ”₯

  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, 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