Cargo is Rust’s package manager and build system. It manages a crate, drives rustc to compile source code, and downloads and compiles dependencies. Unlike compiling a single file directly with rustc, Cargo gives a project a standard structure and commands for common tasks. section 1.13 section 2.1.2
Start with a working program
Run `cargo new hello`, enter the `hello` directory, and run `cargo run`. Cargo creates a project containing `Cargo.toml` and `src/main.rs`. Because no executable exists yet, `cargo run` compiles the starter program in debug mode, then runs it to print “Hello, world!” section 1.3.1
Compare the project before and after its first run: `Cargo.lock` and the `target/debug` build output appear alongside the original source and `Cargo.toml`. section 1.3.1
Use the command that matches the job:
- `cargo new` creates a project in a new directory; `cargo init` sets up the current directory. section 1.6.2
- `cargo build` downloads dependencies and compiles code; `cargo run` builds and executes the result; `cargo doc` generates HTML documentation for project dependencies. section 1.6.2
- `cargo add regex@1` adds version 1 of the `regex` crate as a dependency and updates `Cargo.toml`. section 2.11
`Cargo.toml` records package metadata, including its name, version, and dependencies; Rust source files live in `src`. For an existing single-file program, put the file in a project directory and run `cargo init`. Subsequent `cargo run` commands place compiled executables under `target`. Add `-v` to see the rustc command Cargo runs. section 1.3.1 section 2.1.2
Build a program with dependencies
A command-line clock project uses `cargo new clock`, then adds `clap@2` and `chrono@0.4`. Its `Cargo.toml` lists those dependencies, while `src/main.rs` contains the program. Run `cargo build` to compile it or `cargo run -- --help` to request the program’s help output. section 9.6.4
Cargo can also select an installed compiler toolchain. If a project requires nightly Rust and reports that a feature cannot be used on stable, install nightly with rustup and run it with `cargo +nightly run -q`. section 12.9.4




