Open this lesson in your favourite AI. It'll walk you through the why, explain the demo, and quiz you on the try-it list.
Cargo is unusually batteries-included for a systems language: it's the build tool, package manager, test runner, doc generator, formatter dispatcher, and benchmark runner. cargo new scaffolds a project with a Cargo.toml, a src/, and a git repo all in one command. The build cache (in target/) makes incremental builds fast — but the first clean build of a real project will compile thousands of crates and take minutes. Knowing what cargo build actually does is the first step to making it not painful.
Cargo is both Rust's package manager and its build system — cargo new scaffolds a complete project with a source directory, a Cargo.toml manifest, and a .gitignore in one command. When you run cargo build, Cargo resolves dependencies, compiles every crate in the dependency graph, and places artifacts in target/debug/ or target/release/. Knowing what lives in target/ — the incremental object files, the final binary, the lockfile — demystifies why clean builds take longer and why you should never commit that directory.
cargo new --bin myapp and cargo new --lib mylib. Note the difference (lib has src/lib.rs not src/main.rs).cargo build then cargo build again. Note the second is near-instant — that's the build cache.cargo build --release and compare target/debug/hello vs target/release/hello size and runtime speed.cargo clean — wipes target/. The next cargo build will be slow again. Don't do this in CI without thinking.Use these three in order. Each builds on the one before.
In one paragraph, explain what `cargo build` actually does — invoke rustc, link, what about dependencies?
Walk me through Rust's build cache: where it lives, what it keys on, and why a tiny code change can sometimes trigger a giant recompile.
On a 10-crate workspace, my CI Rust build takes 20 minutes. What are the standard tools to speed it up — sccache, cargo-chef in Docker, mold linker? Compare.
$ cargo new hello
Created binary (application) `hello` package
$ cd hello && tree -L 2
.
├── Cargo.toml
├── Cargo.lock (created on first build)
├── src
│ └── main.rs
└── target (created on first build)
└── debug
$ cat src/main.rs
fn main() {
println!("Hello, world!");
}
$ cargo run
Compiling hello v0.1.0
Finished dev profile [unoptimized + debuginfo] target(s) in 0.5s
Running `target/debug/hello`
Hello, world!
# release profile — much slower compile, much faster binary:
$ cargo run --releasecargo run