A Rust formatter applies rustfmt conventions to Rust source code — 4-space indentation, opening braces on the same line, match arms properly indented, blank lines between function definitions, and proper spacing around ->, =>, and ::. The Rust community uses cargo fmt as the universal standard, and this tool applies the same rules in your browser.
Formatted Rust will appear here...
Example Snippets
How to Use the Rust Formatter
The Rust ecosystem enforces a uniform style via rustfmt, which ships with the standard toolchain. Properly formatted Rust code is expected in all open-source crates and production codebases. This Rust formatter applies the same core rules in your browser without installing Rust or Cargo.
Step 1: Paste Your Rust Code
Copy your Rust source — functions, structs, enums, impl blocks, match expressions, or full modules — and paste it into the input area. The formatter handles use statements, derive attributes, lifetimes, generics, and closure syntax.
Step 2: Choose Indentation
rustfmt uses 4 spaces (selected by default). This is the universal Rust standard. If your project uses a custom rustfmt.toml with different settings, you can choose 2 spaces or tabs from the dropdown.
Step 3: Format or Minify
Click Format Rust to beautify with syntax highlighting. Click Minify to strip comments and collapse whitespace — useful for comparing code sizes before and after refactoring.
rustfmt Key Rules
rustfmt enforces: 4-space indentation, opening brace on the same line as the function/struct/impl, match arms indented one level inside the match block, blank lines between fn and impl method definitions, spaces around -> in return types and => in match arms, and use statements grouped with stdlib first, external crates second, local modules third. Running cargo fmt before commit is standard in the Rust community.
FAQ
Is the Rust formatter free?
Yes, completely free with no signup required. Format unlimited Rust code in your browser at no cost.
Is my Rust code safe?
Yes. All formatting runs locally in your browser using JavaScript. Your code is never sent to any server.
Does this replicate rustfmt exactly?
This tool applies the core rustfmt rules: 4-space indentation, opening brace on the same line, match arms indented, blank lines between fn definitions, and proper spacing around ->, =>, and ::. For production use, run cargo fmt which uses the full rustfmt binary.
What Rust constructs does the formatter handle?
The formatter handles functions, structs, enums, impl blocks, match expressions, trait definitions, use statements, derive attributes, and closures.
Can I minify Rust code?
Yes. The Minify button strips // and /* */ comments and collapses whitespace into a compact format useful for size estimation or diff review.
Can I change the indentation from 4 spaces?
Yes. You can choose 2 spaces or tabs from the indent dropdown, though rustfmt defaults to 4 spaces and the Rust community strongly follows this standard.