cargo-fmt
Format Rust source code with rustfmt
TLDR
Format current package
$ cargo fmt
Format all packages in workspace$ cargo fmt --all
Check formatting without changes$ cargo fmt --check
Format specific package$ cargo fmt -p [package]
Show diff of changes$ cargo fmt -- --emit diff
Format with verbose output$ cargo fmt --verbose
SYNOPSIS
cargo fmt [options] [-- rustfmtoptions_]
DESCRIPTION
cargo fmt formats all bin and lib files of the current crate using rustfmt. Formats .rs files in src/, tests/, examples/, and integration tests. Reformats code in-place by default.
PARAMETERS
--all
Format all packages in workspace--check
Check formatting without modifying files (for CI)-p, --package name
Format specific package--manifest-path path
Path to Cargo.toml-v, --verbose
Verbose output-q, --quiet
Suppress output-- --emit mode
Output mode: files, stdout, diff-- --edition year
Override Rust edition
CONFIGURATION
Create rustfmt.toml or .rustfmt.toml in project root:
$ max_width = 100
tab_spaces = 4
edition = "2021"
tab_spaces = 4
edition = "2021"
CI INTEGRATION
$ cargo fmt --all -- --check
Exits with non-zero code if formatting issues found.CAVEATS
Edition automatically read from Cargo.toml. Direct rustfmt defaults to 2015 edition. Set style_edition in config for consistency.
SEE ALSO
cargo(1), rustfmt(1), cargo-clippy(1)
