b2KIT

Rust Formatter (rustfmt)

Format Rust code following rustfmt conventions with configurable edition and style options.

Tested tool guide Tested browser tools Checked August 16, 2026

What Rust Formatter (rustfmt) does, with a checked example

rustfmt is the official Rust formatter, installed with the Rust toolchain. It is a command-line program that runs locally: it parses a .rs file on your machine and re-emits it with the spacing, indentation, and line breaks defined by the Rust Style Guide - the code is never uploaded anywhere. Run rustfmt file.rs for one file, cargo fmt for a whole crate. The surprise most users hit: it refuses code it cannot parse, so a syntax error or an edition mismatch means the file is left untouched, and it only reflows layout - it never removes unused imports, renames anything, or rewrites comments.

Worked example

A concrete input and expected output from the current implementation.

Input

fn main(){let x:Vec<u32>=vec![1,2,3];println!("{:?}",x);}

Expected output

fn main() {
    let x: Vec<u32> = vec![1, 2, 3];
    println!("{:?}", x);
}

rustfmt put a space before the opening brace, moved the body to its own line at four-space indentation, and normalized spacing: around the =, after the type colon, and after commas inside the vec! and println! arguments. Running the output through rustfmt again returns the same text unchanged.

How the result is produced

1

Parse and re-emit

rustfmt parses the source into a syntax tree and rebuilds each item by applying the layout rules from the Rust Style Guide: token spacing, indentation, where lines break, and whether a call fits on one line within the default 100-column max width. Code it cannot parse is rejected with an error instead of reformatted, so the output is always valid and behavior-neutral.

2

Configuration and flags

A rustfmt.toml or .rustfmt.toml in the crate root tunes decisions: max_width, tab_spaces, use_small_heuristics, and others. --edition selects the language edition (cargo fmt takes it from Cargo.toml automatically), and --check only reports whether the file is already formatted, which is how cargo fmt --check works as a CI gate. Stable rustfmt promises stable output across stable releases for a fixed configuration.

Good uses

  • Normalize a diff before review: run cargo fmt (or rustfmt on one file) so a pull request shows only real changes, and add cargo fmt --check to CI so unformatted code fails the build.
  • Format code that came from outside your project: snippets pasted from tutorials, output from a code generator, or files pulled from another repo, so they match the canonical style before you commit them.
  • Settle style disputes with a committed rustfmt.toml: agreed values for max_width, tab_spaces, and related options make every contributor's editor and the CI pipeline produce identical output.

Limits and checks

  • It needs valid, parseable code. A syntax error, or 2021-only syntax parsed with the default 2015 edition, produces a failed-to-parse error and the file stays exactly as it was - rustfmt will not guess at intent, so fix the error or pass --edition first.
  • Formatting is not cleanup. Nothing semantic changes: unused imports stay, poorly named identifiers stay, clippy warnings remain, and comments are preserved as written rather than rephrased or rewrapped. Any diff rustfmt produces is layout-only.
  • Some constructs are deliberately left alone. macro_rules! bodies are preserved as written, and items marked #[rustfmt::skip] are untouched, so a file can pass cargo fmt --check and still contain non-conforming code in those spots.

Common questions

Why does rustfmt fail to parse my code when cargo fmt handles it fine?

Run directly, rustfmt defaults to edition 2015, so newer syntax (let-else, for example) fails to parse until you pass --edition 2021 or newer. cargo fmt reads the edition from Cargo.toml automatically, which is why it usually works where plain rustfmt on a standalone file does not.

Can rustfmt make my code better or fix style issues?

No. It changes only whitespace and line breaks, never semantics, and it will not remove unused imports, rename identifiers, or silence clippy lints. It guarantees consistent layout, not idiomaticity - lint suggestions and compiler-applicable fixes are the job of cargo clippy and cargo fix, and rustfmt does not attempt them.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools