Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
cargo check
cargo check --all-targets      # also tests, examples, benches
cargo check --workspace

Try it

Rust A type error that check reports

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.

Rust The fixed version

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

Terminal
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

  • check finds type and borrow-checker errors, but a few errors only appear during code generation, such as some const-evaluation overflows or linker problems. A clean check followed by a failing build is rare, not impossible.
  • It does not run tests; use cargo check --all-targets to 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 check after a clean can therefore be slow.
  • check and build keep 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 warnings after cargo clippy, or RUSTFLAGS="-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 check for every iteration while writing code is far cheaper than run; switch to run once the code compiles.