LinuxCommandLibrary

rustfmt

Format Rust code according to style rules

TLDR

Format a file, overwriting the original file in-place

$ rustfmt [path/to/source.rs]
copy

Check a file for formatting and display any changes on the console
$ rustfmt --check [path/to/source.rs]
copy

Backup any modified files before formatting (the original file is renamed with a .bk extension)
$ rustfmt --backup [path/to/source.rs]
copy

Format code using a specific Rust style edition (formatting rules) verbosely
$ rustfmt --style-edition [2015|2018|2021|2024] [[-v|--verbose]] [path/to/source1.rs path/to/source2.rs ...]
copy

Format code using a specific Rust edition (language features and parsing)
$ rustfmt --edition [2015|2018|2021|2024] [path/to/source1.rs path/to/source2.rs ...]
copy

SYNOPSIS

rustfmt [OPTIONS] [FILE_OR_DIR...]

Common usage:
rustfmt main.rs
rustfmt src/
cargo fmt (recommended for projects)

PARAMETERS

-h, --help
    Prints help information.

-V, --version
    Prints version information.

--check
    Checks if formatting is needed without writing changes. Exits with 0 if no changes, 1 if changes are needed.

--emit
    Controls output. `files` (default) writes to files, `stdout` prints to standard output, `diff` prints a diff.

--edition <2015|2018|2021|2024>
    Specify the Rust edition to format for.

--config-path
    Recursively searches for `rustfmt.toml` from this path.

--config
    Overrides specific configuration options inline.

--files-with-diff
    Prints the names of files that would be modified.

--files-without-diff
    Prints the names of files that would not be modified.

-v, --verbose
    Use verbose output.

-q, --quiet
    Suppress output.

--color
    Controls colored output.

DESCRIPTION

rustfmt is a command-line tool designed to automatically format Rust source code files. It enforces a consistent and opinionated style, typically aligned with the official "Rust Style Guide," which helps improve code readability, maintainability, and reduces stylistic debates within development teams. By automatically applying a standard format, it ensures that all code in a project looks uniform, regardless of who wrote it. rustfmt can process individual files, entire directories, or integrate seamlessly into build systems and development workflows. It is a crucial tool for fostering collaboration and ensuring a high level of code quality in the Rust ecosystem. Its strong opinions on formatting aim to reduce cognitive load by making code predictable and familiar.

CAVEATS

rustfmt can significantly change code layout, making careful `git diff` review recommended after initial application. It is opinionated; some stylistic choices are not configurable by design. Requires the Rust toolchain to be installed. Best used consistently across a project, typically via `cargo fmt` to ensure all team members adhere to the same style.

CONFIGURATION

rustfmt can be configured using a `rustfmt.toml` file. This file is typically placed in the project root or a parent directory, and rustfmt will automatically discover it. While many core formatting rules are non-configurable by design to maintain consistency, the `rustfmt.toml` allows users to override a subset of default options to suit specific project needs.

INTEGRATION WITH CARGO

The most common and recommended way to run rustfmt on a Rust project is through the Cargo build tool using the command `cargo fmt`. This command automatically identifies all Rust files within the current Cargo workspace (including crates, binaries, and tests) and formats them, simplifying the process for developers.

HISTORY

rustfmt was developed as an official component of the Rust project's tooling. Its primary goal was to provide a standard, consistent code formatter to reduce stylistic bikeshedding, improve readability, and foster collaboration across the growing Rust ecosystem. It has been actively maintained and updated to align with new Rust language features and evolving community style preferences, becoming an integral part of the Rust development workflow.

SEE ALSO

cargo(1), clippy(1), gofmt(1), prettier(1)

Copied to clipboard