Sub topic 1
Where the Time Goes in a Rust Build
Prerequisites
Assume you know basic Rust syntax, ownership, Cargo commands, and Cargo.toml. This chapter focuses on diagnosis rather than changing application behavior.
The build pipeline
Cargo coordinates the build, but rustc does most of the compilation. It parses source code, expands macros, resolves types and traits, and checks borrow rules. It then generates machine-oriented intermediate code.
LLVM optimizes that code and produces object files. Finally, the linker combines your crate, dependencies, and system libraries into an executable. Large dependency graphs, generic code, heavy optimization, and linking can each dominate.
A change in one crate can invalidate later stages for that crate. A dependency change can rebuild much more of the graph.
Measure before changing settings
Use Cargo's timing report before guessing:
cargo build --timingsCargo writes an HTML report under target/cargo-timings/. Look for crates with long compile durations and whether they ran sequentially or in parallel. Also inspect dependency features:
cargo tree -e featuresThis can reveal an accidentally enabled feature or a large transitive dependency. For generic code generation, cargo llvm-lines can show which functions produce the most LLVM IR.
Use `cargo check` for the inner loop
During editing, use:
cargo checkcargo check performs parsing, type checking, and borrow checking but skips executable code generation and linking. It is usually much faster than cargo build. Run a full build when you need to execute the program or verify linking.
Tune the development profile
Debug information and optimization affect compile time. A useful starting point is:
[profile.dev]
debug = "line-tables-only"
[profile.dev.package."*"]
opt-level = 2Line tables preserve useful file and line information for backtraces while avoiding full variable information. Optimizing dependencies can improve their runtime without optimizing your frequently changed application code. Measure afterward because the best settings depend on the project and compiler.
Exercise
Which command should you try first when you only need compiler feedback after editing Rust code?
Show answer
cargo check, because it skips code generation and linking.