Sub topic 3
Use cargo check for the inner loop
This chapter assumes you can build, run and test a Rust project with Cargo, read a Cargo.toml, and use a terminal. It also assumes you know the rough shape of a Rust build from the earlier chapters: rustc parses and type-checks the code, LLVM generates machine code, and a linker produces the final binary.
Why the inner loop matters
The inner loop is the cycle you repeat all day: edit code, ask the compiler whether it is correct, fix the problems, repeat. Most edits in that cycle do not need a runnable binary. You only want to know whether the code type-checks and whether the borrow checker accepts it.
cargo check is built for that question. It runs the front end of the compiler and skips the expensive back end.
What cargo check does
cargo check runs rustc with the --emit=metadata option. rustc goes through these phases:
- Parsing and macro expansion.
- Name resolution and type checking.
- Trait resolution and borrow checking.
- Writing a metadata file (
.rmeta) that describes the crate's public interface.
It then stops. It does not run LLVM code generation and it does not link anything. Those two phases often take most of the wall-clock time in a build, so skipping them is where the saving comes from.
Because cargo check produces metadata and not object code, downstream crates in a workspace can still be checked. A crate only needs the .rmeta of its dependencies to type-check, not their compiled code.
Using it day to day
Run it the same way you run cargo build:
cargo checkUseful variants:
# Check every target: lib, bins, tests, examples, benches
cargo check --all-targets
# Check one workspace member
cargo check -p core
# Check with a specific feature set
cargo check --no-default-features --features "serde"
# Check test code, including #[cfg(test)] modules
cargo check --testsBy default cargo check only checks the library and binaries. Code under #[cfg(test)], plus integration tests, examples and benchmarks, is skipped unless you pass --tests or --all-targets. This is a common source of surprise: a clean cargo check followed by a failing cargo test build is usually a test-only error.
Watching files with cargo-watch
Running the command by hand after each save is slow in human terms. A file watcher removes that step. cargo-watch is a third-party tool you install once:
cargo install cargo-watch
cargo watch -x checkYou can chain commands. This runs a check, then the tests if the check passed:
cargo watch -x check -x testIf you use rust-analyzer in an editor, you may not need a separate watcher. rust-analyzer runs cargo check on save by default and shows the errors inline. You can also point it at clippy, which gives lints in the same pass:
{
"rust-analyzer.check.command": "clippy"
}Separate target directories to avoid lock contention
Cargo takes a lock on the target directory while it builds. If your editor runs cargo check in the background and you run cargo build in a terminal, one of them waits with the message Blocking waiting for file lock on build directory.
A second problem is cache thrashing. rust-analyzer and your terminal may use different flags, so each run invalidates the other's artifacts and both rebuild.
The fix is to give rust-analyzer its own target directory:
{
"rust-analyzer.cargo.targetDir": true
}With true, rust-analyzer uses target/rust-analyzer. The cost is extra disk space, since dependencies are compiled once per directory. For most projects that trade is worth it.
What cargo check does not tell you
cargo check is fast because it skips work, so it also skips the errors that come from that work. Know which classes of problems it cannot report.
- Linker errors. Undefined symbols, missing system libraries and duplicate symbols appear only at link time.
- Some post-monomorphization errors. Certain errors, such as constant evaluation failures inside generic code, are reported only when the generic is instantiated during code generation.
cargo checkmay pass whilecargo buildfails. - Runtime behavior. Type-correct code can still panic, overflow, or compute the wrong answer.
- Build script and proc-macro output. These do run during
cargo check, because the compiler needs their output to type-check. A failure in them shows up in both commands. - Release-only problems. Code gated on
cfg(debug_assertions)is checked under whichever profile you chose, so check with--releaseif you need the other configuration.
The practical rule: use cargo check for the tight loop, and run cargo build or cargo test before you commit.
Check and build do not share artifacts
A frequent assumption is that cargo check warms up the cache for cargo build. It does not for your own crates. Check writes .rmeta files, and build writes .rlib files and object code. Cargo treats them as separate outputs.
For dependencies the situation is slightly different. Checking a dependency produces only metadata for it, so a later cargo build must still compile the dependency fully. The first cargo build after a long run of cargo check can therefore be slow.
Cargo keeps the artifacts for both in target/debug, so switching between the two commands does not delete anything. Each is rebuilt only when its own inputs change.
Measuring the gain
Do not assume the saving. Measure it on your project with the timing tools from the previous chapter. Make a small edit to a function body in your main crate, then time each command:
touch src/lib.rs && time cargo check
touch src/lib.rs && time cargo buildOn a small crate the difference is modest because linking a small binary is quick. The gap grows with project size, with heavy generic code, and with a slow linker. If cargo check is not much faster than cargo build for your project, the time is going into the front end, not into LLVM or the linker. In that case look at macros, dependencies and crate layout in the later chapters.
The table summarizes the trade-off.
| Command | Type and borrow check | LLVM codegen | Linking | Runs your program |
|---|---|---|---|---|
| `cargo check` | Yes | No | No | No |
| `cargo build` | Yes | Yes | Yes | No |
| `cargo run` | Yes | Yes | Yes | Yes |
Common mistakes
- Treating a green
cargo checkas proof the project builds. Runcargo buildorcargo testbefore pushing. - Forgetting
--all-targetsor--tests, then discovering test errors late. - Running editor checks and terminal builds in one target directory and fighting over the lock.
- Checking with different feature flags from the ones you build with. A feature-gated module that is never compiled during check can hide errors.
Summary
cargo check stops after type checking and borrow checking, which skips LLVM and the linker. Use it for the edit loop, automate it with an editor integration or cargo watch, and give rust-analyzer its own target directory. Remember its blind spots, and verify with a full build before you commit.
Exercise
You run cargo check and it passes. Then cargo test fails to compile with a type error inside a #[cfg(test)] module. Why did the check miss it, and what command would have caught it?
Show answer
By default cargo check only checks the library and binary targets, so code under #[cfg(test)] is not compiled. Running cargo check --tests (or cargo check --all-targets) would have caught the error without a full build.
Exercise
Your editor runs cargo check in the background, and your terminal cargo build often prints "Blocking waiting for file lock on build directory". Name one setting that fixes this and one cost of it.
Show answer
Set rust-analyzer.cargo.targetDir to true so rust-analyzer uses target/rust-analyzer instead of target. The cost is extra disk space and an extra one-time compile of dependencies in the second directory.
Exercise
A project has a green cargo check but cargo build fails during the final step with "undefined reference" errors. Which build phase reported the error, and why did check not report it?
Show answer
The linker reported it. cargo check stops after writing metadata and never runs code generation or linking, so symbol-resolution errors cannot appear.