Loading Rust Formatter...
Please wait a moment

How to Fix Rust Indentation

Rust code pasted from an issue, a chat or an email often arrives with every line pushed to the left. Rust does not care about indentation, but people reading it do. This page puts the 4-space indentation back, the way rustfmt lays out blocks and chains, and leaves everything inside your lines alone.

Step 1

Paste or upload your Rust code

Paste a whole file, an impl block or a single function into the left editor, upload a .rs file, or press Sample to load an example whose indentation has been flattened.

For example, this struct and impl lost most of their indentation and picked up three blank lines in a row:

#[derive(Debug)]
struct User {
      id: u64,
  name: String,
}

impl User {
fn new(id: u64, name: String) -> Self {
User { id, name }
}



    fn label(&self) -> String {
let parts: Vec<String> = vec![self.id.to_string(), self.name.clone()]
.into_iter()
.filter(|p| !p.is_empty())
.collect();
parts.join(" ")
}
}
Step 2

Read the re-indented code

A moment after you stop typing, the right panel shows every block indented with 4 spaces per level. Method chains, where clauses and other lines that continue a statement are stepped in one more level, trailing spaces are removed and runs of blank lines become one. If a bracket, string or comment is not closed, you get the line and column instead of output.

The example above comes back like this, which is also what rustfmt produces for it:

#[derive(Debug)]
struct User {
    id: u64,
    name: String,
}

impl User {
    fn new(id: u64, name: String) -> Self {
        User { id, name }
    }

    fn label(&self) -> String {
        let parts: Vec<String> = vec![self.id.to_string(), self.name.clone()]
            .into_iter()
            .filter(|p| !p.is_empty())
            .collect();
        parts.join(" ")
    }
}
Step 3

Minify if you want it small

Minify removes ordinary comments and the whitespace between tokens and keeps a single space only where two tokens would otherwise run together. Press Format to switch back.

Step 4

Copy or download

Copy puts the result on your clipboard; Download saves it as formatted.rs (or minified.rs). If you only need the code smaller, the Rust Minifier has its own page, and for data work the Rust to JSON converter turns Rust values into JSON.

Frequently Asked Questions

What exactly does this formatter change?

Only the whitespace at the start and end of your lines. Code inside braces, brackets and parentheses is indented 4 spaces per level, a line that continues a statement (a method chain such as .iter().map(), the rest of a long expression, the bounds under a where) goes one level further in, tabs and odd widths are replaced, trailing spaces go, blank lines in a row become one and the file ends with a single newline. Nothing inside a line is rewritten, and lines that sit inside a multi-line string, raw string or block comment are copied exactly.

Is it the same as rustfmt?

No. rustfmt also splits code that is crammed onto one line, rewraps long lines, adds spaces around operators and reorders imports; this page does none of that. If your code was formatted by rustfmt and then lost its indentation in a chat or an email, you usually get rustfmt's layout back. For a project, run cargo fmt before you commit; this page is for quickly cleaning up a snippet you copied from somewhere.

Can formatting change what my program does?

It should not, because only whitespace between lines moves. Strings, raw strings like r#"…"#, byte strings, char literals such as '{', lifetimes, labels, macros, doc comments and numbers like 8901240544102066246u64, 14.50 and 1_000 are copied as you typed them. Before the result is shown, the page reads the output again and checks it holds exactly the same tokens as the input; we also test it by comparing the token stream of real crates with the Rust toolchain and compiling the output with rustc.

Why does it say my code cannot be formatted?

Because rustc would stop at the same spot. The page reports brackets that are never closed or closed with the wrong one, a closing bracket with nothing open, strings, char literals and block comments that never end, and characters Rust does not accept outside a string, such as curly quotes pasted from a word processor. It is not a compiler, so a missing semicolon or a type error still gets through; cargo check will catch those.

What does Minify do?

It removes ordinary comments, line breaks and spaces between tokens, and keeps one space only where two tokens would otherwise merge, as in fn main or & &x. Doc comments (/// and //!) stay, each followed by a line break, because they are part of the code and some crates refuse to build without them. Strings are never touched. The result still compiles; keep the formatted version in your repository.

Is my code uploaded anywhere?

No. Formatting runs in your browser, the code is not sent to our server, and nothing is kept: a reload starts with an empty editor. Uploads are limited to 10 MB, and a file of a few thousand lines is formatted in well under a second.