Skip to content
Latchkey

cargo bench: Usage, Options & Common CI Errors

Compile and run benchmark targets.

cargo bench builds your crate with optimizations and runs benchmark targets. The built-in #[bench] harness is unstable, so most projects benchmark with Criterion on stable Rust.

What it does

Compiles in the bench profile (optimized) and executes registered benchmarks. Arguments after -- pass to the bench harness, mirroring cargo test.

Common usage

Terminal
cargo bench                       # run all benchmarks
cargo bench parse                 # filter by name
cargo bench --no-run              # build benches only
cargo bench -- --save-baseline ci # Criterion baseline

Common CI error: bench attribute needs nightly

Using #[bench] / test::Bencher on stable fails with "error[E0658]: use of unstable library feature test." Fix: either run on the nightly toolchain, or switch to Criterion (a harness = false bench target in Cargo.toml) which runs on stable.

Options

FlagEffect
--no-runCompile benches without running
--bench <name>Run a specific bench target
-- <args>Pass args to the harness

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →