liveBook · Manning

Cargo add

As explained in Learn Rust in a Month of Lunches

cargo add is a Cargo command that adds an external crate dependency to a package manifest.

cargo add is a Cargo command for adding an external crate dependency to a Rust package's Cargo.toml file. section 16.5.1

Adding rand

From a Cargo project, run cargo add rand. Cargo adds rand under [dependencies]. The next cargo run downloads the needed code and makes the crate available to the program. section 16.5.1 section 18.1.2

[package]
name = "rust_book"
version = "0.1.0"
authors = ["David MacLeod"]
edition = "2021"
 
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
[dependencies]
rand = "0.8.5"

Shows the Cargo.toml dependency entry produced for rand. section 16.5.1

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines the command, its manifest effect, and subsequent download behavior without detailing dependency resolution internals.
Examples
Uses cargo add rand and shows rand = "0.8.5" under [dependencies].
What it assumes
Assumes a Cargo project and introduces Cargo.toml as its dependency manifest.
What you can do after
Operate/configure a Cargo project by adding an external crate from the command line.
How it connects
Connects Cargo, Cargo.toml, external crates, crates.io, and cargo run.

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 Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.