LinuxCommandLibrary

cargo-fmt

Format Rust code automatically

TLDR

Format all source files

$ cargo fmt
copy

Check for formatting errors without writing to the files
$ cargo fmt --check
copy

Pass arguments to each rustfmt call
$ cargo fmt -- [rustfmt_args]
copy

SYNOPSIS

cargo fmt [OPTIONS] [<PATH>…]

PARAMETERS

--all, --all-features
    Format all packages in workspace, ignoring members

--check
    Check if code is formatted, do not modify files

--color <WHEN>
    Control output coloring: auto, always, never

--config <KEY=VALUE>
    Override Cargo.toml configuration values

--doc
    Also format documentation sources

--edition <EDITION>
    Rust edition for formatting (default: 2021)

--emit <EMIT>
    Emit mode: files, stdout, coverage (default: files)

--frozen, --locked
    Do not update Cargo.lock during execution

--help, -h
    Display help information

--manifest-path <PATH>
    Path to Cargo.toml

--package <SPEC>…
    Format specific package(s)

--quiet, -q
    Suppress cargo log messages

--root <PATH>
    Path to workspace root

--verbose, -v, -vv
    Increase verbosity of output

<PATH>…
    Specific paths to format

DESCRIPTION

cargo fmt is a subcommand of Cargo, Rust's build system and package manager, designed to automatically format Rust source code according to the conventions enforced by the rustfmt tool. It ensures consistent styling across projects, improving readability and maintainability without altering code semantics.

Invoked within a Rust project directory containing a Cargo.toml file, it targets .rs files in standard locations like src/, tests/, examples, and integration tests. By default, it reformats code in-place. Key features include support for Cargo workspaces, allowing selective formatting of packages via --package or entire workspaces with --all.

The --check flag is invaluable for continuous integration (CI) pipelines, as it verifies formatting compliance without modifying files, exiting with a non-zero code if issues are found. Output can be directed to stdout (--emit stdout) or coverage reports generated (--emit coverage). Configuration inherits from rustfmt.toml, Cargo.toml's [fmt] section, or command-line overrides.

cargo fmt automatically installs the matching rustfmt version if absent, leveraging Rust's toolchain manager (rustup). It respects edition settings (--edition) and can format documentation (--doc). Widely used in Rust development for pre-commit hooks and IDE integrations.

CAVEATS

Requires Rust toolchain; auto-installs rustfmt but may fail without rustup. Ignores non-Rust files. Configuration conflicts between rustfmt.toml and Cargo.toml possible.

CONFIGURATION

Settings from rustfmt.toml or Cargo.toml [fmt] section take precedence; use --config for overrides.

EXAMPLES

cargo fmt (formats project)
cargo fmt --check (CI check)
cargo fmt --all --package mylib (workspace package)

HISTORY

Introduced in Cargo 0.20 (2017) to integrate rustfmt (stable since 2016). Evolved with Rust editions; --check added for CI in 0.24. Now essential for Rust style enforcement.

SEE ALSO

rustfmt(1), cargo(1), rustup(1), clippy(1)

Copied to clipboard