Rust Formatter
Format Rust source with rustfmt, the same tool cargo fmt runs. Edition-aware, and it reports the line and column when the code does not parse.
Rust cannot be formatted by a regular expression or by a browser tokenizer: a macro invocation takes an arbitrary token tree, lifetimes and generics are ambiguous without the parser, and an attribute may contain a whole expression. rustfmt is the answer by construction, so this page runs it on the server and returns exactly what cargo fmt would write. A syntax error comes back as the compiler's own message with a line and a column, not as a guess.
Paste Rust source, pick an edition and press the button: rustfmt runs on the tool server and returns the same program formatted the way cargo fmt would format it. The page also runs its sample through rustfmt as it loads, so the panel is not empty when you arrive.
This is the compiler's own formatter, not a token re-indenter. Macros take arbitrary token trees, lifetimes and generics are ambiguous without a parser, and an attribute can contain a whole expression, so a regular-expression tidy-up would eventually change a program. rustfmt parses and re-emits instead.
A formatter is not a compiler: the page does not build the crate, resolve dependencies, check types or run tests. A file with a syntax error is refused with the position rustfmt reported, rewritten as a line and column because the input is a textarea rather than a file.
What the edition changes
The edition decides a few parsing and formatting rules, so a 2015 crate should be formatted as 2015 even when the toolchain also understands 2024. The page offers 2015, 2018, 2021 (the default) and 2024, and an unknown value is refused.
rustfmt runs in an empty temporary directory, so it cannot pick up this server's own rustfmt.toml. That makes the output depend on the source and the edition, not on where the process happens to live.
- Editions: 2015, 2018, 2021 or 2024
- Up to 200,000 characters and ten seconds
- No project rustfmt.toml is read
What it will not do
The output is formatting only. Unused imports, clippy lints, missing documentation and type errors all survive, because none of them is a layout question. If the result equals the input the info line says nothing changed, which is a real answer rather than a failure.
A project with a custom rustfmt.toml may indent differently, since this page offers no configuration beyond the edition. For a repository that must match its own CI, run cargo fmt there rather than trusting a server's defaults.
How to use the formatter
Paste the source into the Rust Source panel and choose the crate's edition from the dropdown: 2015, 2018, 2021 or 2024, with 2021 selected on arrival. Press the format button and the Formatted panel shows what rustfmt returned for that edition.
When the input does not parse, the page reports rustfmt's position rewritten as a line and column instead of a formatted file; fix the syntax and run again. An info line says when the output matches the input, so an unchanged result needs no further action.
What to Expect
Best for
- Formatting a snippet before pasting it into a Rust project
- Checking how rustfmt wraps a long match or a chain of calls
- Formatting a file when no toolchain is installed locally
Not ideal for
- Linting, type checking or building the crate
- Matching a project whose rustfmt.toml differs from the defaults
- Formatting a generated file over 200,000 characters
What this tool keeps
- The program's meaning, because the source is parsed and re-emitted
- Comments and attributes, which rustfmt preserves
- The edition you selected, which affects the rules applied
What may need cleanup
- A syntax error, which must be fixed before rustfmt will format anything
- Indentation that a project's own configuration would set differently
- Macro bodies, which rustfmt formats conservatively
Common errors
- Pasting a fragment with an unclosed brace and reading the position as a tool error
- Expecting clippy suggestions from a formatter
- Choosing an edition the crate does not use and finding the layout slightly different
Example use cases
- Cleaning up a code sample for documentation
- Formatting a snippet from a bug report
- A first look at rustfmt's default style
Sample input
A word-count program written on one line with a HashMap, formatted as edition 2021.
Sample output
The same program with its statements on separate lines and standard indentation, plus an info line saying whether anything changed.
Who this is for
- Rust developers without a local toolchain
- Reviewers preparing a minimal diff
- Learners comparing rustfmt's output
Frequently asked questions
What does the Rust formatter do?
rustfmt runs on the tool server and returns the same program formatted the way cargo fmt would, at the edition you choose.
Does it lint or build the crate?
No. It formats only, so linting, type checking and building are separate.
Why does my project's own configuration change the output?
The page uses default rustfmt rules, so a project with its own rustfmt.toml may format differently.
Build custom tools for your store
Join the SDK program to create useful ecommerce utilities for your store.