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

SYNOPSIS

rustfmt [options]

PARAMETERS

--version
    Show rustfmt's version.

--config-path
    Specify the path to the rustfmt.toml configuration file.

--emit
    Specify how rustfmt emits its output (files, stdout, stderr, etc.).

--edition
    Rust edition to use. Specify like 2015, 2018 or 2021.

--print-config
    Prints the default configuration for a specified file type like toml.

--verbose
    Enable verbose output.

--quiet
    Suppress error messages.

--stdio
    Format code from standard input and output to standard output.

--write-mode
    How rustfmt writes the formatted output back to the files (overwrite, display, etc.).


    Paths to the Rust files or directories you want to format.

DESCRIPTION

rustfmt is a tool to automatically format Rust source code according to a consistent style. It aims to enforce a single, unified style across Rust projects, eliminating stylistic debates and making code more readable and maintainable.

It automatically reformats code, handling spacing, indentation, line breaks, and other stylistic elements to adhere to the Rust style guide.

rustfmt is typically used as part of a project's build process, or through a pre-commit hook, ensuring all code checked into the repository conforms to the standard style. Configuration files can be used to customize rustfmt's behavior. While rustfmt aims for a consistent style, some options allow projects to tailor formatting to their specific needs, although divergence from the standard style is generally discouraged. rustfmt supports many common Rust constructs and automatically adjusts your code to fit the conventions.

CAVEATS

rustfmt cannot always perfectly format code, especially with complex macros or unconventional syntax. Sometimes, manual adjustments may be required after running rustfmt. Also, extremely large files can take a while to process.

CONFIGURATION

rustfmt's behavior can be configured via a rustfmt.toml file placed in the project directory (or a parent directory). This file allows for customization of formatting options like indentation size, line width, and brace style.

INTEGRATION

rustfmt integrates well with various IDEs and text editors through plugins and extensions. It can also be used as a pre-commit hook in Git repositories to automatically format code before commits.

HISTORY

rustfmt was created to address the lack of a standardized code formatting tool for Rust. It has evolved significantly over time, incorporating more sophisticated formatting rules and becoming an integral part of the Rust development ecosystem. The tool is actively maintained and updated to support new Rust features and to refine the formatting algorithms.

SEE ALSO

cargo(1)

Copied to clipboard