Rust Formatter
Paste Rust with broken indentation and get it re-indented from brace depth, with long runs of blank lines cut to one.
Input
Output
What is Rust?
Rust is a systems programming language focused on safety, speed, and concurrency. It prevents memory bugs at compile time through its ownership system and is widely used for systems programming, WebAssembly, and high-performance applications.
This page does one job: it re-indents. Each line is trimmed, a line ending in {, ( or [ pushes the following lines four spaces deeper, and a line starting with }, ) or ] pulls back out. Runs of blank lines shrink to one. It will not split a one-line impl block or add a space after a colon; that is the job of rustfmt.
How to Use
Paste your Rust code
Paste a function or a whole module into the left pane, or open an .rs file with Upload. Sample loads a small UserRepository built on a HashMap.
Formats as you type
The right pane refreshes a moment after you stop typing. Depth comes from brackets alone, four spaces for each line that ends open, so } else { and match arms ending in => { land where you expect.
Copy or download
Take the result with Copy, or press Download for a main.rs file. If the file is headed for a crate with its own rustfmt.toml, run cargo fmt on it afterwards.
Formatting Example
Example: Format a Rust struct
Unformatted input:
struct User {
name: String,
age: u32,
}
impl User {
fn new(name: String, age: u32) -> Self {
User { name, age }
}
fn greet(&self) -> String {
format!("Hello, {}!", self.name)
}
}Formatted output:
struct User {
name: String,
age: u32,
}
impl User {
fn new(name: String, age: u32) -> Self {
User { name, age }
}
fn greet(&self) -> String {
format!("Hello, {}!", self.name)
}
}Frequently Asked Questions
What is rustfmt?
rustfmt is the official Rust code formatter. This tool provides quick online formatting for review purposes. For production use, run rustfmt locally as part of your CI/CD pipeline.
Does Rust use spaces or tabs?
The official Rust style guide uses 4 spaces for indentation, which this formatter follows. See The Rust Book and Rust by Example for more on Rust conventions.
Can I format Rust macros?
The formatter handles basic block indentation. Complex macros may require rustfmt for accurate formatting.
Is my Rust code safe when I use this tool?
Yes. The re-indent is one loop over the lines in this page's script, with no fetch or upload anywhere in it, so a main.rs that still has a database URL or token in a const stays on your machine.