liveBook · Manning

Cargo

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

Cargo is Rust's package manager and build system for creating, compiling, running, documenting, and managing Rust projects.

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

$ tree --dirsfirst hello 
hello
├── src
│   └── main.rs
├── target
│   └── debug
│       ├── build
│       ├── deps
│       ├── examples
│       ├── native
│       └── hello
├── Cargo.lock
└── Cargo.toml

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

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

How deep it goes
Walks through project creation, files, commands, dependencies, and a verbose rustc invocation rather than Cargo’s own internals.
Examples
Uses a Hello World project, a regex-powered search project, and a command-line clock.
What it assumes
Starts with an installed Rust toolchain and console access; teaches project creation before requiring the reader to write Rust code.
What you can do after
Operate/configure it to create projects, add dependencies, build, run, generate documentation, and inspect compiler commands.
How it connects
Connects Cargo to rustc, rustup, crates, dependencies, and Cargo.toml.

Also explained in

Compare how 5 books explain Cargo →

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.