LinuxCommandLibrary

cargo-check

Check Rust code for errors quickly

TLDR

Check the current package

$ cargo [[c|check]]
copy

Check all tests
$ cargo [[c|check]] --tests
copy

Check the integration tests in tests/integration_test1.rs
$ cargo [[c|check]] --test [integration_test1]
copy

Check the current package with the features feature1 and feature2
$ cargo [[c|check]] [[-F|--features]] [feature1,feature2]
copy

Check the current package with default features disabled
$ cargo [[c|check]] --no-default-features
copy

SYNOPSIS

cargo check [OPTIONS]

Common invocation patterns:
cargo check
cargo check --release
cargo check --target x86_64-unknown-linux-gnu

PARAMETERS

--target
    Check for the specified target triple (e.g., x86_64-unknown-linux-gnu).

--release
    Check with optimizations, indicating a release build profile. This also disables debug assertions.

--workspace
    Check all packages in the current workspace.

-p , --package
    Check only the specified package. This may be a package ID specification.

--bin
    Check only the specified binary target.

--example
    Check only the specified example target.

--test
    Check only the specified test target.

--bench
    Check only the specified benchmark target.

--all-targets
    Check all available targets (bins, examples, tests, benches, and the library).

--features
    Space-separated list of features to activate for the current package.

--all-features
    Activate all available features for the current package.

--no-default-features
    Do not activate the default features for the current package.

-q, --quiet
    Do not print cargo log messages.

-v, --verbose
    Use verbose output, showing more details about the process.

--color
    Control when colors are used in output. Possible values: 'auto' (default), 'always', 'never'.

--message-format
    Output compiler messages in a specific format (e.g., 'json', 'json-diagnostic-rendered-ansi').

DESCRIPTION

cargo check is a Rust command-line tool that performs a fast, non-linking compilation of a local package and all its dependencies. Unlike cargo build, it does not produce an executable or a library. Its primary purpose is to validate code for compilation errors, warnings, and correctness issues without incurring the overhead of code generation and linking. This makes it significantly faster for iterative development, allowing developers to quickly catch syntax errors, type mismatches, and other basic compilation failures. It's often used as a preliminary step before cargo build or cargo test to ensure the code is syntactically and semantically sound.

CAVEATS

Does not produce an executable; it's purely a syntax and type checking pass.
While faster than cargo build, it still performs full type checking and can be slow on very large codebases.
It will not catch runtime errors or issues related to linking.
Requires a Cargo.toml file in the current directory or a parent directory to identify the project.

INTEGRATION WITH IDES

Many Rust IDE plugins or language servers (e.g., rust-analyzer) use cargo check under the hood to provide immediate compilation errors and warnings as you type, significantly enhancing the development experience by offering real-time feedback.

HISTORY

cargo is the official Rust package manager and build system, introduced alongside the Rust programming language. The check subcommand was added relatively early in cargo's development (around the Rust 1.10 timeframe, released in July 2016) to address the need for a faster compilation feedback loop. Before check, developers often had to run cargo build to validate code, which was slower due to code generation and linking. cargo check was designed specifically to provide quick feedback on correctness, making it an essential tool for rapid iterative development in Rust.

SEE ALSO

cargo build(1), cargo test(1), cargo run(1), cargo clippy(1), cargo fmt(1)

Copied to clipboard