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 rustfmtCommon 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
| Flag | Effect |
|---|---|
| --all | Format every workspace member |
| --check | Report diffs, exit non-zero, no writes |
| -- --edition <ed> | Set the rustfmt edition |
Related guides
cargo clippy: Usage, Options & Common CI Errorscargo clippy runs the Rust linter. Learn how to deny warnings in CI with -D warnings, target specific lints,…
cargo check: Usage, Options & Common CI Errorscargo check type-checks your crate without producing a binary, making it the fast CI gate. See usage, --works…
go fmt: Usage, Options & Common CI Errorsgo fmt formats Go source with gofmt. Learn how to gate CI on formatting with gofmt -l and why go fmt rewrites…