Sub topic 3
Use cargo check for the inner loop
This chapter assumes you know how to run cargo build and cargo test, and that you have a rough idea of the rustc, LLVM and linker phases from the first chapter. Here you will use cargo check to make the edit-compile loop shorter.
What cargo check skips
Most edits only need an answer to one question: does this code still compile? Answering it does not require a binary. cargo check runs the rustc front end: parsing, macro expansion, name resolution, type checking and borrow checking. It writes metadata (.rmeta files) and stops. It never runs LLVM code generation or the linker, which are often the slowest phases.
Running it
# Check the current package
cargo check
# Include tests, examples and benches
cargo check --all-targets
# Check every crate in a workspace, or just one
cargo check --workspace
cargo check -p corePlain cargo check skips code under #[cfg(test)]. A test module with a type error passes until you add --all-targets or --tests.
Where it fits
In practice the loop looks like this:
- Edit code.
- Run
cargo checkuntil the errors are gone. - Run
cargo testorcargo runonly when you need to execute something.
A watcher saves you from typing the command each time. Both tools below are installed with cargo install.
# Re-run check whenever a file changes
cargo watch -x check
# Or use bacon, which shows a compact error list
baconEditors do this for you. rust-analyzer runs cargo check on save by default. If you also run builds in a terminal, the two can wait on each other because they share the target directory lock. A separate target directory for the editor avoids that wait:
{
"rust-analyzer.cargo.targetDir": true
}With true, rust-analyzer uses target/rust-analyzer instead of target.
Common errors and surprises
- Check passes, build fails. Some errors only appear during code generation or linking. Examples are unresolved linker symbols and errors found after generic code is instantiated for concrete types. Run a real build before you push.
- Check does not warm the build.
cargo checkandcargo buildwrite different artifacts. The firstcargo buildafter a long run of checks still compiles everything. Do not expect check to prepare for build. - Build scripts and proc macros still run. Check has to expand macros, so it compiles and runs
build.rsfiles and procedural macro crates. A slow build script slows check too. - Dependencies are checked as well. On a clean target directory, the first
cargo checkcompiles every dependency's front end. Later runs reuse that work. - Different flags mean a rebuild. Changing features, profile or
RUSTFLAGSbetween runs invalidates the cached results. Pick one set of flags for your inner loop.
Measuring the difference
Time both commands after a one-line edit. Add a comment to src/main.rs and run each command in turn:
touch src/main.rs && time cargo check
touch src/main.rs && time cargo buildThe gap grows with project size and with the amount of generic code, since code generation scales with both. A small project may show only a modest difference. Large projects often show a big one.
Exercise
A teammate says "cargo check passed, so CI's cargo build can't fail." Give two reasons this can be wrong.
Show answer
First, check does not run LLVM code generation or the linker, so errors from those phases (such as unresolved linker symbols) only appear in a real build. Second, plain cargo check skips #[cfg(test)] code, so type errors in tests go unnoticed unless you run cargo check --all-targets. Different feature flags or profiles in CI can also expose errors that the local check never compiled.