FAQ - Help and questions

How do I add, remove and update dependencies with Cargo?

#cargo#dependencies#toolingpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Run cargo add name to add a crate to Cargo.toml, cargo remove name to take it out, cargo update to move Cargo.lock to the newest versions the version requirements allow, and cargo tree to see what each dependency pulls in. Commit Cargo.lock for programs so everyone builds the same versions.

Checked with cargo 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

A dependency has two records. Cargo.toml states which versions you accept (serde_json = "1.0.154" means 1.0.154 or any later 1.x). Cargo.lock records the exact versions that were chosen, including the dependencies of your dependencies. Cargo edits both for you.

How to do it#

  1. Add a crate. Cargo looks up the newest version, writes the line, and lists the crate's optional features.
text
> cargo add serde_json
    Updating crates.io index
      Adding serde_json v1.0.154 to dependencies
             Features:
             + std
             - alloc
             - arbitrary_precision
             - float_roundtrip
             - indexmap
             - preserve_order
             - raw_value
             - unbounded_depth
    Updating crates.io index
     Locking 11 packages to latest Rust 1.93.0 compatible versions

[dependencies]
serde_json = "1.0.154"
  1. Use it, and run.
src/main.rsrust
fn main() {
    let v: serde_json::Value = serde_json::json!({"ok": true});
    println!("{v}");
}
text
> cargo run -q
{"ok":true}
  1. See what it brought along. Everything below serde_json is a transitive dependency that you did not ask for.
text
> cargo tree
depdemo v0.1.0 (C:\dev\depdemo)
└── serde_json v1.0.154
    ├── itoa v1.0.19
    ├── memchr v2.8.3
    ├── serde_core v1.0.229
    └── zmij v1.0.23

cargo tree -i itoa (invert) answers the opposite question, "who needs this crate?":

text
itoa v1.0.19
└── serde_json v1.0.154
    └── depdemo v0.1.0 (C:\dev\depdemo)
  1. Remove it again. cargo remove edits Cargo.toml and the unused crates leave Cargo.lock the next time Cargo runs.
text
> cargo remove serde_json
    Removing serde_json from dependencies
name = "depdemo"
version = "0.1.0"
edition = "2024"
  1. To pick up newer compatible versions of everything, run cargo update. For one crate: cargo update -p serde_json.

Why it works#

A version requirement like "1.0.154" is shorthand for ^1.0.154: compatible updates are allowed, breaking ones are not. That is why cargo update is normally safe, and why moving to a new major version (2.0) is an edit you make yourself.

Common mistakes#

  • Not committing Cargo.lock for an application. Without it, two builds a month apart can use different versions. (Libraries commonly leave it out, because the user's own lock file decides.)
  • Naming a package like a folder Cargo uses. A binary called deps is refused, because target/debug/deps already exists.
  • Adding a crate for a one-line job. Every dependency is more code to compile and to trust.

In Ruststudio#

Run > Cargo: Add Dependency... adds a crate, Run > Cargo: Update Dependencies updates, and Run > Cargo: Show Dependency Tree shows the tree. Under Tools, Cargo: Security Audit (cargo audit), Cargo: Check Outdated Dependencies, Cargo: Find Unused Dependencies (cargo machete) and Cargo: Check Licenses and Bans (cargo deny) run those tools; they must be installed.

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet