Cargo is Rust’s package manager and build system. It creates projects, manages dependencies, and drives the Rust compiler, rustc, to build executable programs. section 1.3.1 section 2.1.2
With Rust installed, create and run a first project from a command prompt: section 1.3.1
These commands create the hello project, enter its directory, and run its starter program. section 1.3.1
The new project has a Cargo.toml file for metadata, including its name, version, and dependencies. Its source code is in src/main.rs. When you run the project, Cargo compiles the starter program in debug mode and executes it; the executable is placed under target/debug. section 1.3.1 section 2.1.2
Useful Cargo commands include:
- cargo new creates a project in a new directory; cargo init initializes one in the current directory. section 1.6.2
- cargo build downloads dependencies and compiles the code. cargo run builds and then runs the executable. section 1.6.2
- cargo doc builds HTML documentation for the current project’s dependencies. section 1.6.2
- cargo run -v shows more detail, including how Cargo invokes rustc. section 2.1.2
Add a dependency
For a search project, run cargo new grep-lite --vcs none and cd grep-lite. Then run cargo add regex@1 to add version 1 of the regex crate to Cargo.toml, followed by cargo build to download and compile it. If cargo add is unavailable, it may need separate installation. section 2.11 section 9.6.4
Some projects need a particular compiler toolchain. For a project that requires nightly Rust, install it with rustup and use cargo +nightly run to select it. section 12.9.4





