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:
- Create or enter a Cargo project, such as grep-lite or mandelbrot. section 2.11 section 2.7
- Run cargo add regex@1 to add version 1 of the regex crate, or cargo add num to add num. section 2.11 section 2.7
- Run cargo build or cargo run; Cargo retrieves and compiles the dependency before compiling the project. section 2.11 section 2.7
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
Shows the command output that confirms cargo add inserted num v0.4.0 into the project's dependencies. section 2.3.4

