TExcelTAD
Home › Cargo in Depth › Cargo in Depth: What Cargo Does

Cargo in Depth: What Cargo Does

Most Rust developers meet Cargo as three commands: cargo new, cargo build, cargo run. That is enough for a hobby project. It stops being enough the day a CI build breaks because a dependency released a new version, a workspace member suddenly compiles with a feature nobody asked for, a release binary is twice as big as it should be, or a crate has to link a C library and cross-compile to an ARM board. This course is about that day.

Who this course is for

You can write Rust: modules, use, pub, structs, traits and error handling. The Rust course covers that, and its lesson Rust Modules & Cargo introduces cargo new and Cargo.toml; this course starts where it stops. The general ideas behind every build tool (dependency graphs, semantic versioning, lockfiles, caching, cross-compilation) are taught language-neutrally in Build Fundamentals. You don't need it first, but you'll recognise its ideas here.

Cargo's five jobs

Job What Cargo does Where you'll learn it
Describe reads Cargo.toml: the package, its targets, dependencies, features and profiles Cargo.toml Section by Section
Resolve turns version requirements into one exact version per crate and records it in Cargo.lock Version Requirements, Cargo.lock
Build runs build scripts, then calls rustc once per crate in dependency order, with the right flags and features Profiles, build.rs
Test and document cargo test, cargo doc, cargo bench, plus the check, clippy and fmt front ends Targets, cargo test & cargo doc
Ship packages the crate, publishes it to a registry, vendors dependencies for offline builds Publishing, Vendoring

Cargo itself never compiles Rust code. It is an orchestrator: it plans the build graph and invokes rustc (and your build scripts, and the C compiler through them). Keeping that picture in mind explains a lot of its behaviour.

A first session

Every bash run session in this course is a real terminal session, recorded with Rust and Cargo 1.99.1 on Linux. Here is the smallest one: create a package, run it, then run it again.

bash · try it yourself
cargo new hello
cd hello
cat Cargo.toml
cargo run
cargo run
ls target/debug
    Creating binary (application) `hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[package]
name = "hello"
version = "0.1.0"
edition = "2024"

[dependencies]
   Compiling hello v0.1.0 (/home/learner/hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s
     Running `target/debug/hello`
Hello, world!
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/hello`
Hello, world!
build
deps
examples
hello
hello.d
incremental

Three things to notice:

  • cargo new wrote a manifest with the 2024 edition, an empty [dependencies] table, and created a Git repository (pass --vcs none to skip it; the rest of the course does, to keep listings short).
  • The first cargo run printed Compiling hello; the second did not. Cargo fingerprints every unit it builds (source files, flags, dependency versions) and skips the ones that are up to date. Finished names the profile used: dev, unoptimised with debug info.
  • Everything Cargo produces goes under target/. target/debug/hello is the program; deps/ holds compiled dependencies, build/ build-script output, incremental/ the compiler's incremental cache.

How the sessions in this course work

The machine that recorded the sessions has no network. crates.io is replaced (with the same mechanism you'll use for vendoring) by a local copy that holds a fixed set of real crates: serde, serde_json, rand 0.10 and 0.8, anyhow, thiserror 2 and 1, clap, cc, regex and their dependencies. A rand = "0.10" dependency resolves and locks exactly as it would against crates.io, but asking for a version that isn't there fails. There is also a private registry called local that you start with crate-registry start and publish to.

When Cargo fetches a crate from that local copy it prints Unpacking (against crates.io you'd see Downloading and Downloaded instead). Timings such as in 0.15s will differ on your machine.

The course map

  1. Manifest & lockfile: what each table in Cargo.toml does, how "1.2" turns into 1.3.0, why Cargo.lock exists, how two versions of the same crate end up in one build.
  2. Features: optional code and dependencies, and the unification rule that makes them additive.
  3. Workspaces: many packages, one lockfile, one target/, and inherited settings.
  4. Profiles and build scripts: optimisation, LTO and panic, then build.rs, compiling C with the cc crate and generating code.
  5. Targets and commands: bins, examples, tests, benches, docs, check, clippy, fmt.
  6. Shipping: .cargo/config.toml, vendoring, offline builds, packaging, publishing, yanking and cross-compiling.
  7. Toolkit, field notes, exercises and a mini project: a workspace with a library, a clap CLI and a C dependency, published to a private registry and consumed from another project.

Tip: Cargo's own documentation, The Cargo Book (doc.rust-lang.org/cargo), is excellent. When a lesson names a manifest key or a flag, the Cargo Book has the full reference.

Test yourself

Which program actually compiles Rust source code when you run cargo build?

  1. cargo
  2. rustc, invoked by Cargo once per crate
  3. rustup
  4. the linker

Why did the second cargo run not print Compiling hello?

  1. Cargo caches only release builds
  2. Cargo's fingerprint for the crate was unchanged, so the unit was up to date
  3. cargo run never recompiles
  4. The binary was downloaded

Where does cargo build put the debug binary of package hello?

  1. ./hello
  2. target/debug/hello
  3. bin/hello
  4. ~/.cargo/bin/hello