Rust Formatter: Readable Rust Code Before rustfmt
Rust code from AI generators, documentation, or Stack Overflow is often flat or inconsistently indented. This formatter applies brace-depth indentation to struct, impl, fn, enum, and trait blocks — making Rust code readable before you have a Rust toolchain installed.
Derive attributes (#[derive(Debug)]) are kept at column 0, following Rust convention.
Rust's Block Structure
Rust uses { } for all blocks: struct bodies, impl blocks, function bodies, match arms, if/else, loop, while, for, and closure bodies. The formatter tracks brace depth across all of these, applying consistent indentation. Struct literal initializations on a single line have balanced braces and don't change the indentation of the next line.
Attributes and Derive Macros
Rust attributes like #[derive(Debug, Clone)] and #[cfg(test)] are kept at column 0. They sit directly above the item they annotate and are not part of a block.
rustfmt for Production Code
Rust's official formatter is rustfmt, run via 'cargo fmt'. It enforces 4-space indentation, line length limits, trailing commas, and more. Most Rust CI pipelines include 'cargo fmt -- --check'. Use this browser tool for inspection; use rustfmt for committed code.
Practical Examples
Reading a Rust Library's Source Code
Format a flat copy of Rust code from documentation to understand its structure.
- 1.Paste the Rust source (may have no indentation)
- 2.Formatter applies brace-depth indentation to struct/impl/fn
- 3.Review ownership patterns, lifetime annotations, and trait implementations
- 4.Copy into your editor to continue experimenting
What Gets Formatted
- struct, impl, fn, enum, trait, mod blocks
- if/else, match, loop, while, for, closure blocks
- Derive/outer attributes (#[...]) kept at column 0
- 2 or 4 space indentation toggle
Good Use Cases
- Formatting AI-generated Rust code snippets
- Making Rust documentation examples easier to read
- Quick review before adding cargo to a project
- Teaching Rust ownership and struct patterns
Frequently Asked Questions
Does this match rustfmt output?
No. rustfmt (cargo fmt) is the official Rust formatting tool and handles line length, trailing commas, match arm alignment, and many style details. This browser formatter only applies brace-depth indentation — useful for quick readability.
Are derive attributes (#[derive(...)]) indented?
No. Derive attributes (#[derive(Debug, Clone)]) and other outer attributes are kept at column 0, matching the Rust convention of placing attributes directly above their items.
How do I run rustfmt on my project?
Run 'cargo fmt' in your Rust project directory to format all files according to the official Rust style guide. For a single file, use 'rustfmt file.rs'. Add rustfmt checks to CI with 'cargo fmt -- --check'.
Does this handle macro invocations like vec![]?
Macro invocations that use { } (like closure-like macros) are counted for indentation. Macros using () or [] are treated as normal expressions and don't affect depth.