Rust Reference ยท Tools
rustfmt.toml configuration
Reference for rustfmt.toml: stable options such as max_width, edition and tab_spaces, where the file is found, and how to skip formatting with attributes.
rustfmt.toml (or .rustfmt.toml) holds the configuration for rustfmt and cargo fmt. Each line is option = value. Most projects need no file at all, because the defaults are the community standard; a file is for the few settings a team really wants to change.
rustfmt looks for the file in the directory of the source file being formatted and then in each parent directory, stopping at the first one it finds. This page is a read-only reference; the examples show code shaped by two of the options.
max_width = 100 edition = "2024" use_field_init_shorthand = true
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
Common stable options (option, default):
| Option | Description |
|---|---|
max_width (100) |
Maximum line width. |
hard_tabs (false) |
Indent with tabs instead of spaces. |
tab_spaces (4) |
Spaces per indent level. |
edition ("2015" or from Cargo) |
Language edition used to parse; cargo fmt fills it from the manifest. |
newline_style ("Auto") |
Auto, Unix, Windows or Native. |
use_small_heuristics ("Default") |
Off or Max to change when short constructs stay on one line. |
use_field_init_shorthand (false) |
Rewrite x: x as x. |
use_try_shorthand (false) |
Rewrite try! to ? (old code only). |
reorder_imports, reorder_modules (true) |
Sort use and mod items alphabetically within a block. |
match_block_trailing_comma (false) |
Add a comma after block arms of match. |
Many other options (import grouping, comment wrapping, imports_granularity) are unstable and only apply on nightly; stable rustfmt prints a warning and ignores them.
Usage notes
rustfmt --print-config current .(ordefault) prints the effective configuration.- Per-item control:
#[rustfmt::skip]on an item or statement,#![rustfmt::skip]for a file, and#[rustfmt::skip::macros(name)]for a specific macro. - Keep options minimal. Every changed option makes your code look different from most open-source Rust.
- Format-on-save in editors uses the same file; commit it to share the layout across the team.
- CI should run
cargo fmt --checkwith the same toolchain version as developers, since rustfmt output can shift slightly between releases. - The file can set
ignore(nightly) to skip paths; on stable, use#![rustfmt::skip]or amodoutside the formatted tree. - Unknown option names are errors, which is useful for catching typos.
Related
- cargo fmt, cargo clippy, Cargo.toml
- Tutorial: Clippy and rustfmt