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.
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.
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.