Rust and Cargo 1 cheatsheet

Rust and Cargo 1: Testing and code quality

9 common Rust and Cargo 1 tasks.

Run the tests

Run unit tests, integration tests and doc tests.

cargo test

Run matching tests

Run tests whose names contain the text.

cargo test parse_config

Show test output

Show what tests print. Output is hidden for passing tests by default.

cargo test -- --nocapture

Run benchmarks

Run benchmarks. The built-in harness needs the nightly toolchain, so most projects use criterion.

cargo bench

Format the code

Format every file with rustfmt.

cargo fmt

Check formatting in CI

Fail if any file isn't formatted, without changing files.

cargo fmt --check

Lint the code

Run clippy and treat warnings as errors, as many CI pipelines do.

cargo clippy -- -D warnings

Apply compiler suggestions

Automatically apply fixes the compiler suggests.

cargo fix

Check a crate before publishing

Package and verify a crate without uploading it.

cargo publish --dry-run

Was this page helpful?