Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Tools

cargo add, cargo remove and cargo update

Reference for cargo add, cargo remove and cargo update: add dependencies, pick features, understand version requirements and refresh Cargo.lock safely.

Three Cargo subcommands manage the dependencies of a package. cargo add writes a new entry into the [dependencies] table of Cargo.toml, cargo remove deletes one, and cargo update changes which exact versions are recorded in Cargo.lock without touching the manifest.

The manifest stores a version requirement such as "1.4", which means "any compatible 1.x release at or above 1.4.0". The lock file stores the one concrete version Cargo picked. Understanding that split explains almost everything these commands do. Our editor compiles single files with rustc, so the commands below are read-only terminal sessions; the example code models the version rules in plain Rust.

Terminal
$ cargo add serde --features derive
$ cargo remove serde
$ cargo update -p serde

Try it

Rust Caret requirements, modeled in Rust

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust What cargo update picks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Terminal
cargo add <crate>[@<version>] [options]
cargo remove <crate>... [options]
cargo update [<spec>...] [options]
Name Description
cargo add [email protected] Adds serde = "1.0". Without @version Cargo asks the registry for the latest release.
-F, --features a,b Enables features of the dependency; --no-default-features turns its defaults off.
--dev, --build Add to [dev-dependencies] or [build-dependencies] instead.
--optional Marks the dependency optional (it becomes an implicit feature).
--path, --git Add a local or Git dependency instead of a registry one.
-p, --package Pick the workspace member to edit or update.
cargo update -p name Update only that package (and what it forces). --precise 1.2.3 pins an exact version.
--dry-run Show what would change; write nothing.

Errors and edge cases: a crate name that does not exist on the registry fails; cargo update never crosses a major version, because your manifest forbids it.

Usage notes

  • cargo add keeps your comments and formatting in Cargo.toml; it edits, it does not rewrite the file.
  • Commit Cargo.lock for applications and binaries so builds are reproducible. Libraries may commit it too, but downstream users ignore it.
  • cargo update with no arguments refreshes every dependency within its requirement; it is the usual first step when a security advisory lands.
  • Use cargo update -p foo --precise 1.2.3 to roll back a bad release. If another crate requires a newer one, Cargo explains the conflict.
  • To move across a major version, edit the requirement (or cargo add foo@2), then run cargo update -p foo.
  • cargo remove also cleans up empty feature references it can see, but check [features] by hand for leftovers.
  • Features are additive across the whole dependency graph: one crate enabling a feature turns it on for everyone. See Cargo profiles and features.
  • cargo add was added to Cargo itself in 1.62 (before that it was the separate cargo-edit crate); cargo remove arrived in 1.66.