LinuxCommandLibrary

cargo-fmt

Format Rust source code with rustfmt

TLDR

Format current package

$ cargo fmt
copy
Format all packages in workspace
$ cargo fmt --all
copy
Check formatting without changes
$ cargo fmt --check
copy
Format specific package
$ cargo fmt -p [package]
copy
Show diff of changes
$ cargo fmt -- --emit diff
copy
Format with verbose output
$ cargo fmt --verbose
copy

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"
copy

CI INTEGRATION

$ cargo fmt --all -- --check
copy
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

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community