Rust Code Formatter
Re-indent Rust source code by tracking brace, paren and bracket nesting depth — fn, struct, impl, trait, enum, match arms and closures get consistent indentation, while comments, string and raw-string literals are left untouched.
Input
Formatting Options
Output
Guides
The Rust Code Formatter re-indents messy or hand-pasted Rust source so every block lines up consistently. Paste your code (or upload a .rs file), choose an indent size, and the formatted result appears instantly with copy and download buttons.
It works by tracking nesting depth through the braces, parentheses and brackets in your code. Each fn, struct, impl, trait, enum, match arm, if/else block and closure body is indented one level deeper than the construct that contains it, and a line that begins with a closing delimiter is lined up with the line that opened its block. Whatever indentation the input had is discarded and rebuilt from scratch.
What it does — and what it doesn't
This is a structural re-indenter, not a drop-in replacement for rustfmt. The real rustfmt is a native Rust binary that parses the full language grammar; it can't run in your browser. This tool deliberately does one thing well — fix indentation — and leaves everything else on each line untouched:
- It does recompute the leading whitespace of every line from brace/paren/bracket depth, so nested code is cleanly stepped in and out.
- It does not rewrite spacing around operators, wrap long lines to a column limit, sort
useimports, or insert or remove trailing commas.
If you need byte-perfect, canonical formatting for a commit, run cargo fmt locally. If you just want to make a chunk of copied Rust readable again, this tool is faster and needs nothing installed.
Comments and string literals are safe
A naive re-indenter breaks the moment a brace appears inside a string or a comment. This formatter uses a Rust-aware scanner, so brackets are only counted when they are real code. It correctly skips over:
- line comments (
//,///,//!), - block comments (
/* ... */), including the nested block comments Rust allows, - normal and byte string literals (
"...",b"..."), including ones that span several lines, - raw string literals (
r"...",r#"..."#,br##"..."##) with matched hash counts, - character literals (
'a','\n','\u{7f}'), which are told apart from lifetimes like'aand'static.
Text inside a multi-line string or comment is emitted exactly as you typed it, so a formatter run never changes the value of a string.
Which indent size should I use?
Four spaces is the Rust community standard and the default here (it matches rustfmt). Two spaces and tabs are offered for codebases or snippets that prefer them.
Is my code sent anywhere?
No. All formatting runs entirely in your browser — your code never leaves your device.