Rust Formatter (rustfmt)

Paste your Rust and get it back in the standard Rust style. The formatter is rustfmt, the same tool that ships with Rust, running in your browser, so your code stays on your device.

Indent
Max width
Edition

Format uses rustfmt, the official Rust formatter. Options apply to Format.

Result

What rustfmt does

rustfmt applies the standard Rust style. It fixes indentation, tidies spacing, wraps long lines to fit your width, and groups and orders use declarations so every Rust file looks the same. This tool runs rustfmt in your browser and changes only the layout, never what the program does.

Why use our Rust formatter

  • ·Free. No signup, no paywall, no limits.
  • ·Runs in your browser. rustfmt runs locally as WebAssembly, so your code is never uploaded.
  • ·The real rustfmt. Output matches what cargo fmt produces on your own machine.
  • ·No setup. No install and no Rust toolchain needed to format a quick snippet.
  • ·Syntax highlighting. The editor colours keywords and types, and lets you collapse and expand blocks.

How rustfmt formats Rust

Format

Rewrites your Rust with rustfmt, the official Rust formatter. It fixes indentation, spacing, and line breaks to match the standard Rust style.

Beforefn main(){let x=1;println!("{}",x)}Afterfn main() { let x = 1; println!("{}", x) }

Max width

Set the line width rustfmt aims for. Its default is 100 characters. Pick a smaller width for narrow screens or a larger one to keep more code on one line.

BeforeLong call on one lineAfterWrapped to fit the width

Edition

Choose the Rust edition of your code (2015, 2018, 2021, or 2024). This tells rustfmt which syntax to expect so newer code parses and formats correctly.

BeforeEdition 2015 defaultAfterParsed as your edition

When to format Rust

rustfmt helps whenever you have Rust code that is not yet in the standard style. These are the cases that come up most:

  • ·Quick formatting without a toolchain. Format a snippet when Rust is not installed on the machine you are on.
  • ·Cleaning up pasted code. Fix the layout of Rust copied from a forum, a blog post, or a chat.
  • ·Checking a snippet. See how rustfmt would lay out a piece of code before you paste it in.
  • ·Learning Rust. See the standard style while you are still learning the language.

Where to go from here with Rust

New to the language? Start at rust-lang.org, then read The Rust Book, the free guide that walks you through the language from the ground up.

Want to change how the output looks? Every setting rustfmt understands, and what each one does, is written up in the rustfmt configuration guide.

If you work across languages, these formatters run the same way, right in your browser:

Frequently asked questions

What is a Rust formatter?
A Rust formatter takes Rust source code and rewrites it in the standard Rust style, with correct indentation, spacing, and line breaks. This tool uses rustfmt, the official formatter that ships with Rust, so the result matches what cargo fmt would produce on your machine.
How do I format Rust code?
Paste your Rust into the input box and click Format. The clean version appears below. Click the copy button to copy it, or download it as a .rs file. You can also set the indent, max line width, and edition before formatting.
What Rust edition should I pick?
Pick the edition your project uses, which is set in Cargo.toml (for example edition = "2021"). If you are not sure, 2021 is a good default for recent code. The edition tells rustfmt which syntax to expect, so newer features parse correctly.
Does rustfmt use tabs or spaces?
rustfmt indents with spaces, four per level by default. You can change the width to 2 spaces in the options if you prefer a tighter layout.
Does formatting change how my Rust program works?
No. rustfmt only changes whitespace, line breaks, and the order of imports. Your types, functions, and logic stay the same, so the program builds and runs exactly as before.
Is my Rust code kept private?
Yes. The formatter is rustfmt compiled to WebAssembly and runs entirely in your browser. Your code is never uploaded to a server or saved anywhere, so you can safely paste in code from a private project. Once the page has loaded you can even use it offline.