Appendix D · The Rust toolchain.
Place rustc, Cargo, rustfmt, Clippy, rustdoc, and tests in one mental model.
The idea behind the syntax.
Rust's toolchain is a collection of tools with different responsibilities. rustc compiles source; Cargo coordinates packages, dependencies, builds, and tests. rustfmt applies a consistent layout. Clippy reports many common mistakes and style concerns. rustdoc turns documentation comments into browsable API documentation and can execute examples embedded in them. None of these tools replaces the others.
A typical feedback sequence can start with a fast compile check, then move to tests, linting, documentation, and a release build. The sequence is useful because each stage answers a different question. A check can confirm the code is type-correct while tests reveal behavioral gaps; a release build can expose a configuration or performance difference that a debug build hides.
What Rust is checking.
Cargo reads the manifest and lockfile to choose dependencies and features. The edition is declared in Cargo.toml, while the installed compiler version determines which language and standard-library features are available. A clean environment may need to download crates, so reproducibility depends on dependency versions and availability as well as your own source files.
Tool output is evidence to interpret, not a verdict on design quality. A Clippy warning can identify a likely issue, but an intentional exception should be explained close to the code. A passing test suite covers only the cases it contains. The examples below include a manifest-related explanation, tests, and compiler feedback, showing how the toolchain helps a reader inspect one program from several angles.
See the rule in context.
Each example isolates one part of the model. Follow the bindings, types, ownership transfers, and possible outcomes line by line.
Cargo manifests & dependencies
Cargo.toml declares package metadata, an edition, and dependencies. Cargo.lock records resolved dependency versions for reproducibility and should be committed for these executable workbook projects. An edition selects language rules and migration boundaries; it is not the compiler version. cargo check type-checks without a final executable, cargo test runs tests, cargo fmt formats, and cargo clippy offers additional diagnostics. Features are additive configuration switches: inspect which optional capabilities a dependency enables instead of adding every feature by default.
fn main() {
let manifest = "[package]\nname = \"first-build\"\nversion = \"0.1.0\"\nedition = \"2024\"\n";
println!(
"{}",
manifest
.lines()
.find(|line| line.starts_with("edition"))
.unwrap()
);
}edition = "2024"
- A manifest is configuration rather than Rust source.
- The workbook generates a proper manifest per lesson.
- Run cargo check, cargo test, and cargo fmt --check from the workspace root as appropriate.
The boundary: A semver requirement such as 1 allows compatible updates. A committed lockfile pins the actual resolved graph until updated.
Unit tests & assertions
A function annotated with #[test] is discovered by the Rust test harness. Tests normally live in a cfg(test) module and can access private code in their parent module. assert_eq! shows both sides on failure; Result-returning tests can use ? for setup. Test behavior rather than the incidental implementation. Include empty input, boundary values, and failure cases that matter to the function’s contract. A passing example is useful evidence, but it cannot establish correctness for every possible input.
fn clamp(n: i32) -> i32 {
n.clamp(0, 100)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn handles_edges() {
assert_eq!(clamp(-1), 0);
assert_eq!(clamp(101), 100);
}
#[test]
fn keeps_in_range() {
assert_eq!(clamp(50), 50);
}
}
fn main() {
println!("{} {}", clamp(-5), clamp(120));
}0 100
- cfg(test) includes the test module only in a test build.
- cargo test or rustc --test runs the test harness.
- The ordinary binary still has its main function.
The boundary: Tests sharing process-global state can interfere when the harness runs them in parallel. Prefer isolated inputs and temporary resources.
Compiler messages, fmt & Clippy
Compiler diagnostics show a primary error, relevant source locations, and often a suggested explanation. Read the ownership or type relationship before changing syntax at random. cargo check gives quick type feedback; rustc --explain E0382 explains a moved-value error. cargo fmt standardizes layout and cargo clippy provides additional lints. Debug output and dbg! help inspect a value during development, but logs should be intentional before release. A small reproducible example often reveals the actual contract problem faster than a large rewrite.
fn borrow_len(s: &str) -> usize {
s.len()
}
fn main() {
let name = String::from("Rust");
let bytes = borrow_len(&name);
println!("{name}: {bytes} bytes");
}Rust: 4 bytes
- The function borrows because it only needs a read-only view.
- The owner remains available for printing.
- Try changing the parameter to String locally to inspect how a move changes the contract.
The boundary: Do not blindly accept a clone suggestion without deciding whether shared access or independent ownership is required.
Keep this model.
Each tool asks a different question about a Rust program. Use their combined evidence to understand the code, not to assume that one green status proves the whole system correct.