liveBook · Manning

Cargo

As explained in Code Like a Pro in Rust

Cargo is Rust's official build system and package manager.

Cargo is Rust’s package manager and build tool. It provides the usual interface to the compiler, the crates.io registry, and project commands; using rustc without Cargo is possible, but rarely the practical default. section 1.1 section 2.0

Start with the command that matches the job:

  • Run cargo new NAME for an application or cargo new NAME --lib for a library. Cargo creates Cargo.toml and an entry point at src/main.rs or src/lib.rs, respectively. section 2.1.2
  • Use cargo check during rapid edits: it validates code without producing final targets. Use cargo build to compile and link, cargo run to compile and execute, and cargo test to compile and run tests. section 2.1.3
  • Declare crates under [dependencies] in Cargo.toml or use cargo add. Use cargo update to refresh Cargo.lock within the declared version requirements. section 2.2

Add a dependency

A manifest entry of [dependencies] followed by rand = "0.8" requests a compatible 0.8 release. Cargo uses caret requirements by default when no operator is supplied. An exact requirement such as =0.8.0 pins a version; avoid unnecessary pins in libraries when downstream packages may need different versions. section 2.2

Table 2.1 Cargo build and run commands (view table figure)

Cargo command

Summary

build

Compiles and links your package, creating all final targets

check

Similar to build, except does not actually generate any targets or objects—merely checks the validity of code

test

Compiles and runs all tests

run

Compiles and runs the target binary

Compare build, check, test, and run by whether each produces targets or executes code. Prefer check when final output is not yet needed: its time savings can accumulate across repeated edits, though a single measured build showed only a modest difference. section 2.1.3

Cargo also supports project publication, documentation generation with cargo doc, and automated build, test, and publish steps in CI/CD. Modules and workspaces help organize larger projects. For compiler caching, an optional setup installs sccache with cargo install and selects it through RUSTC_WRAPPER before building. section 2.13 section 3.5.1

How Code Like a Pro in Rust covers it

How deep it goes
Works through project scaffolding, command selection, and dependency requirements, including iteration costs and version-pinning trade-offs.
Examples
Uses generated application and library layouts, a rand dependency declaration, and timed cargo build versus cargo check runs.
What it assumes
Starts from generated application and library templates, requiring a working Cargo installation rather than an existing project.
What you can do after
Operate/configure it to create Rust projects, choose build and test commands, manage dependencies, and generate documentation.
How it connects
Connects rustc, crates.io, Cargo.toml, Cargo.lock, semantic versioning, workspaces, rustdoc, and CI/CD.

Also explained in

Compare how 5 books explain Cargo →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.