Rust Reference ยท Tools
cargo check
Reference for cargo check: type-check and borrow-check a Rust package without generating code, why it is faster than build, options, and use in CI and editors.
cargo check runs the compiler through parsing, name resolution, type checking and borrow checking, then stops before generating machine code. It reports exactly the same errors and warnings as cargo build, but finishes much sooner because the slow code generation and linking steps are skipped.
It is the command to run on every save while you are working, and the one editors call behind the scenes (rust-analyzer uses it by default). It writes metadata files (.rmeta) into target/, not a runnable program. The terminal commands below are read-only; the examples show the kind of mistakes check catches.
cargo check cargo check --all-targets # also tests, examples, benches cargo check --workspace
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
cargo check [options]
| Option | Description |
|---|---|
--all-targets |
Check libraries, binaries, tests, examples and benchmarks (plain check skips tests). |
--tests, --lib, --bin <name>, --example <name> |
Select targets. |
--workspace, -p <name> |
Select packages. |
--features, --all-features, --no-default-features |
Feature selection; each combination is a separate check. |
--release |
Check with the release profile settings (separate cache). |
--profile test |
Check as a test build. |
--message-format json |
Machine-readable diagnostics for tools. |
--locked, --offline |
Reproducibility and network control. |
Usage notes
checkfinds type and borrow-checker errors, but a few errors only appear during code generation, such as some const-evaluation overflows or linker problems. A cleancheckfollowed by a failingbuildis rare, not impossible.- It does not run tests; use
cargo check --all-targetsto make sure test code at least compiles. - Build scripts and procedural macros still have to be compiled and run, because the crate cannot be analyzed without them. The first
checkafter a clean can therefore be slow. checkandbuildkeep separate caches. Alternating between them does not lose work, but the two do not share each other's output.- Lints work the same way. Add
-- -D warningsaftercargo clippy, orRUSTFLAGS="-D warnings", to make CI fail on warnings. - In CI a sensible order is
cargo fmt --check,cargo check --all-targets,cargo clippy,cargo test; the cheapest failures come first. - Using
checkfor every iteration while writing code is far cheaper thanrun; switch torunonce the code compiles.