Logo

MonoCalc

/

Rust Code Beautifier

Programming

Re-prints the source from its syntax tree with the style options below.

Load a sample

Ctrl/Cmd+Enter runs · Ctrl/Cmd+K clears

Rust source

Formatted Rust

Style

rustfmt uses spaces.
1–8 spaces per level; rustfmt uses 4.
Wrap target (40–200); rustfmt's max_width is 100.
Same-line braces are idiomatic Rust.

The parser, the printer and the lexer all run in this tab. Your source is never uploaded, logged or executed.

About This Tool

Rust Code Beautifier - Format, Minify and Validate Rust Online

Rust rarely reaches you in the shape its author left it. It arrives compressed into a chat message, flattened into a single line inside a JSON payload, half-pasted from a forum answer, or dumped by a build log with the indentation stripped out. This Rust code beautifier re-emits it in the layout the ecosystem settled on: four-space indentation, same-line braces, one item per line, spaces around operators, and trailing commas in multi-line lists.

The usual answer is rustfmt, but that is a Rust binary and needs a working toolchain, which is a poor trade for nine lines from a forum post and may not be an option at all on a locked-down machine. This tool runs entirely inside your browser tab - no upload, no server round-trip, no compilation.

How it works

Beautifying parses your source into a real syntax tree and prints it again from scratch, so the output reflects structure rather than a guess at it. That distinction is what keeps &'a str, Vec<Box<dyn Trait>>, parse::<i32>() and |x| x + 1 intact where a brace-counting beautifier would mangle them. Struct and enum definitions get one field per line, and match arms line up with normalised => spacing. A method chain of several calls is broken one call per line whatever your width setting is, which is the printer's own rule rather than a consequence of the width.

Minifying and validating use a separate Rust lexer built into the tool. Every comment and every literal - "text", r#"raw"#, b"bytes", 'c' and their escapes - is treated as an opaque atom that is copied through byte for byte. A brace inside a string, a // inside a URL, or a quote inside a raw string is never mistaken for structure.

Three modes

Beautify expands compressed Rust back into readable form using your indent style, width and brace preference. Minify goes the other way, collapsing the source to the smallest text that still lexes identically - useful for embedding a snippet in a single-line string, a shell heredoc or a JSON field. It inserts a space only where leaving one out would fuse two tokens, which is why let x keeps its space while a + b becomes a+b. Validate only runs the structural checker and rewrites nothing.

What the validator catches

Unbalanced {}, () and [], reported at the line and column where the delimiter was opened rather than where the file ran out; delimiters that close in the wrong order; unterminated strings; raw strings closing with the wrong number of # characters; block comments left open, including the nested /* /* */ */ form Rust allows; and character literals holding more than one codepoint, without mistaking a lifetime such as 'a or a loop label such as 'outer for one.

Structure, not semantics
The validator is a lexer and a delimiter checker, not a compiler. It will tell you that a paste was truncated mid-string; it will not tell you that a type is wrong, a borrow outlives its owner, or a name does not resolve. Code can pass here and still fail cargo check.

The safety guard

Formatting is only allowed to move whitespace. To check that rather than assume it, every run lexes the printed output again and compares its token stream with the input's, token by token. Two differences are recognised as safe and named rather than hidden: a trailing comma, which Rust treats as optional before a closing delimiter, before the > of a generic list and at the end of a where clause; and parentheses the printer adds to spell out grouping it already parsed, turning 1 << 3 >> 2 into (1 << 3) >> 2. Anything else discards the result and hands your input back unchanged, with a message saying so.

Two constructs are refused rather than reformatted
The bundled parser predates C string literals such as c"text", stabilised in Rust 1.77, and would silently split one in two, so Beautify refuses when it sees one - Minify and Validate handle them correctly. A nested tuple index written with a space, t.0 .1, is caught by the guard for the same reason. Both are named on screen instead of being quietly mangled.

Reading the numbers

The change list is measured from the two texts rather than inferred from the options, so it reports how many lines were actually re-indented and how many token boundaries were actually respaced - never a transformation that did not fire. Alongside it sit line, character and byte counts for both sides, the deepest nesting reached and how many lines still exceed your width target.

Style notes

Rust has an unusually strong style consensus, worth knowing before you change the defaults. Four spaces and same-line braces come from rustfmt's defaults. The 100-column max_width is a wrapping target, not a hard limit: a long string, path or comment cannot be broken without changing the program, so those lines are left alone. Trailing commas earn their keep in code review - adding a field touches one line instead of two. Next-line (Allman) braces are offered because people ask, but they are not the Rust convention.

Sorting imports

Import grouping puts standard library paths first, then external crates, then crate, self and super, alphabetised within each group and separated by a blank line - the grouping rustfmt's group_imports = "StdExternalCrate" option produces. It moves only whole use statements that sit on lines of their own, and skips any carrying an #[cfg(…)] attribute or a comment directly above, because moving one would silently re-attach it to a different import.

Frequently Asked Questions

Is the Rust Code Beautifier free?

Yes, Rust Code Beautifier is totally free :)

Can I use the Rust Code Beautifier offline?

Yes, you can install the webapp as PWA.

Is it safe to use Rust Code Beautifier?

Yes, any data related to Rust Code Beautifier only stored in your browser (if storage required). You can simply clear browser cache to clear all the stored data. We do not store any data on server.

How does this Rust beautifier work?

Your source is parsed in the browser into a real syntax tree and then printed again from scratch with the indentation, width and brace style you pick. Because the output is generated from the parsed structure rather than by nudging braces around with regular expressions, lifetimes, generics, turbofish, closures, raw strings and match arms come out correctly instead of being mangled. Minify and Validate use a separate Rust lexer built into the tool, which treats every comment and literal as an opaque atom.

Is my code uploaded anywhere?

No. The parser and the printer are JavaScript that runs inside your own tab, with no server round-trip and no logging, and nothing is compiled or executed - the tool only reads your code and prints it back. That matters for Rust in particular, where a pasted module routinely carries internal crate paths, endpoints and the occasional key nobody remembered to strip.

Is this the same as rustfmt or cargo fmt?

No, and it cannot be. rustfmt is a Rust binary that needs a local toolchain; there is no toolchain in a browser tab. This tool uses a JavaScript Rust parser and printer that follow the same conventions rustfmt made standard - four-space indentation, same-line braces, a 100-column target, trailing commas in multi-line lists - so the result is close in spirit and perfectly consistent. It will not be byte-identical to cargo fmt, so do not use it to satisfy a CI check that runs the real thing.

Can formatting change what my code does?

Printing only moves whitespace, so it should not. To check rather than assume, every run lexes the printed output again and compares its token stream with the input's, token by token, and the badge above the output reports the verdict. Two differences are recognised as safe and named rather than hidden - a trailing comma, and parentheses the printer adds to spell out grouping it already parsed. Anything else discards the result and hands your input back untouched instead of returning something subtly different.

Does it catch compile errors?

Only structural ones. The validator reports unbalanced braces, parentheses and brackets with the line and column where the problem started, plus unterminated strings, raw strings with mismatched hashes, unterminated nested block comments and malformed character literals - the things that break a truncated or half-pasted snippet. It does not do type checking, borrow checking or name resolution, so code can pass validation and still fail to compile.

How are macros, raw strings and other literals handled?

Raw strings, byte strings, character literals and escapes are copied through byte for byte and never reformatted inside; a brace or a quote in a string is never mistaken for structure. Macro invocations such as vec! and println! are spaced like ordinary calls, while the body of a macro_rules! definition is left as written, because its contents are token trees rather than expressions. Two constructs the bundled parser predates are refused outright rather than mangled: C string literals such as c"text", added in Rust 1.77, and a nested tuple index written as t.0 .1.