liveBook · Manning

Cargo add

As explained in Rust in Action: Systems programming concepts and techniques

cargo add is a Cargo subcommand that adds a dependency declaration to a Rust project's manifest.

cargo add is a Cargo subcommand that adds a third-party crate to a Rust project's dependency declarations. It edits Cargo.toml for you, avoiding manual dependency changes. section 2.11 section 2.3.4

Use cargo add while building a project:

Adding a crate to a small program

From a new project directory, run cargo new grep-lite --vcs none, cd grep-lite, and cargo add regex@1. Cargo updates Cargo.toml; cargo build then downloads and compiles regex and its supporting crates. The program can use regex afterward. section 2.11

$ cargo add num 
    Updating 'https:/ /github.com/rust-lang/crates.io-index' index
      Adding num v0.4.0 to dependencies

Shows the command output that confirms cargo add inserted num v0.4.0 into the project's dependencies. section 2.3.4

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains the command, manifest edit, and subsequent compilation, but stops short of its implementation internals.
Examples
Uses grep-lite with regex and a Mandelbrot renderer with num as hands-on dependency examples.
What it assumes
Assumes a command prompt and a Cargo project, while teaching dependency addition through concrete Rust projects.
What you can do after
Operate/configure Cargo by adding dependencies and then building or running projects that use them.
How it connects
Connects Cargo, Cargo.toml, crates, dependencies, cargo build, cargo run, and cargo-edit.

Also explained in

Compare how 2 books explain cargo add →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.