Overview
Introduction
Rust code pasted from documentation or a non-rustfmt-formatted source often has indentation that doesn't match its brace structure.
This tool reindents each line based on `{ }` nesting depth, entirely in your browser, without requiring the Rust toolchain.
What Is Rust Formatter?
A lightweight reindenter that tracks brace, parenthesis, and bracket depth line by line and re-indents accordingly.
It's a whitespace-only pass: tokens and logic are never touched, only leading indentation.
How Rust Formatter Works
Each line is scanned for `{}/()/[]` characters to compute a running nesting depth.
Lines that start with a closing brace are dedented one level before printing, matching their opening brace.
When To Use Rust Formatter
Use it when a Rust snippet's indentation has drifted after manual edits.
It's a quick visual check that braces are balanced.
Often used alongside C Formatter, Go Formatter and Java Formatter.
Features
Advantages
- Runs entirely client-side with no Rust toolchain required.
- Fixes indentation drift instantly for typical brace-delimited code.
- Handles arbitrarily deep nesting without any configuration needed.
Limitations
- Not a real parser: brace characters inside string literals, lifetimes, or comments still affect depth.
- For rigorous style enforcement, use `rustfmt` locally, which the Rust community expects verbatim.
Examples
Best Practices & Notes
Best Practices
- Use this only for a quick visual cleanup; always run the real `rustfmt` before committing Rust code.
- Check whether the final line's indentation returns to depth zero, that's a quick sign no closing brace is missing.
Developer Notes
Implemented as a single-pass line scanner that tracks a running brace/paren/bracket balance; it does not tokenize strings, lifetimes, or comments.
Rust Formatter Use Cases
- Quickly cleaning up indentation in a pasted Rust snippet before running rustfmt
- Spotting an unbalanced brace by eye
Common Mistakes
- Committing this tool's output directly instead of running it through the real `rustfmt`.
- Assuming brace characters inside string literals, macros, or comments are ignored, they aren't; the scanner counts every `{`, `}`, `(`, `)`, `[`, or `]` regardless of context.
Tips
- Always pipe the final code through `rustfmt` locally; this tool is a stopgap for quick browser-based cleanup, not a replacement.
- Diff the reindented output against your original before running rustfmt, so you can see exactly which lines shifted.