Skip to content
Latchkey

cargo fmt: Usage, Options & Common CI Errors

Format Rust code to a consistent style.

cargo fmt runs rustfmt over your crate. In CI you run it with --check so it reports - but does not apply - formatting differences and exits non-zero when code is unformatted.

What it does

Invokes rustfmt on every target in the package (or workspace with --all), rewriting files in place unless --check is passed. Arguments after -- go to rustfmt.

Common usage

Terminal
cargo fmt                         # format in place
cargo fmt --all                   # format all workspace crates
cargo fmt --check                 # CI: fail if unformatted
cargo fmt -- --edition 2021       # pass an option to rustfmt

Common CI error: code is not formatted

CI fails on cargo fmt --check because a contributor did not run formatting; rustfmt prints a diff and exits 1. Fix: run cargo fmt --all locally and commit the result. If the failure is "error: rustfmt is not installed", add it with rustup component add rustfmt.

Options

FlagEffect
--allFormat every workspace member
--checkReport diffs, exit non-zero, no writes
-- --edition <ed>Set the rustfmt edition

Related guides

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