Introduction: Let the Compiler Show Its Work
Rust is easiest to learn when you stop treating compiler errors as interruptions. They are observations about a program: a value moved here, a reference must remain valid there, or a generic function asked for an operation it never required.
This book teaches those relationships through short predictions. Each chapter follows the same loop:
- Read a small program.
- Predict whether it compiles or what it prints.
- Inspect the actual result.
- Build a working mental model.
- Refine that model into the precise rule.
- Compare two valid fixes.
- Solve one variation.
Do not merely read the snippets. Put them in src/main.rs and run cargo check or cargo run. For deliberate failures, read the first relevant diagnostic before looking at the answer.
The six questions
Most of the book can be navigated with six questions:
- Who owns this value?
- Is this operation a move, copy, borrow, or reborrow?
- How long must this reference remain valid?
- Is mutation happening while another path can observe the value?
- Does the compiler know the concrete type and required capabilities?
- Is this state absence (
Option) or failure (Result)?
These are more useful than memorizing individual error codes. They transfer from tiny exercises to real programs.
Setup
Install stable Rust with rustup, then verify:
$ rustc --version
rustc 1.92.0 (ded5c06cf 2025-12-08)
$ cargo --version
cargo 1.92.0 (344c4567c 2025-10-21)
Create a scratch program:
$ cargo new rust-scratch
$ cd rust-scratch
$ cargo run
Use these commands throughout:
cargo check: type-check quickly without producing the final executable.cargo run: build and execute a binary.cargo test: run checks marked with#[test]and documentation tests.cargo fmt: apply standard formatting.cargo clippy --all-targets -- -D warnings: reject suspicious code and warnings.
The companion crate at the root of this repository contains corrected examples and one integrated test. It deliberately has no dependencies. The standard library is enough to learn the language rules.
What this book does not do
It does not survey every standard-library API, teach a web framework, or prescribe an application architecture. Those subjects change and are better learned when a project requires them. The goal here is durable: make Rust's ownership, type, and concurrency rules predictable.
Start with Part I even if you already know another systems language. Rust references are not merely pointers with friendlier syntax; their types encode aliasing and lifetime constraints that shape APIs throughout the rest of the language.