How to Minify Rust Code
Add your Rust code
Paste a .rs file into the left editor, upload one with Upload, or press Sample to try the example program.
Let it minify
About half a second after you stop typing, comments and spacing are removed and the result appears on the right with its size before and after in UTF-8 bytes. Code that cannot be read, such as an unclosed string or bracket, shows an error with the line and column instead.
Copy or download
Copy puts the minified code on your clipboard and Download saves it as minified.rs. Both use exactly what is in the editor at that moment.
The rules for which tokens may touch come from the Rust reference on tokens. If you want the opposite, readable code with consistent indentation, use the Rust Formatter.
Frequently Asked Questions
Will the minified code still compile and behave the same?
For ordinary code, yes. Only whitespace and comments are removed; every token stays the same and in the same order, and the result is read back before it is shown to make sure nothing merged. Two things do change: line!(), column!() and panic messages report the new positions, and doc comments are dropped, so rustdoc pages and doctests for that file go away.
Which doc comments are kept?
Doc comments inside a macro call stay, because a macro sees them as #[doc] attributes and may need them to match. A file that mentions missing_docs keeps all of its doc comments too. If your lib.rs has #![deny(missing_docs)] and you minify a module file on its own, that file loses its docs and the crate will refuse to build, so keep the originals for those files.
Are strings, lifetimes and numbers left alone?
Yes. String literals, raw strings like r#"…"#, byte and C strings, char literals and lifetimes are copied byte for byte, including the spaces and any // inside them. Numbers are not re-spelled either: 8901240544102066246, 0x1F_u8 and 14.50_f64 come out exactly as typed.
Why are some spaces still in the output?
Some spaces are needed. Two words like x as i64 must stay apart, return "x" needs its space because Rust 2021 reserves word"…" as a literal prefix, and Vec<Vec<u8>> = keeps one so the brackets do not become >>=. Inside macro calls such as println!, assert! and stringify! a single space is kept wherever you had one, since those macros can print your code back with its spacing.
How much smaller will my file get?
It depends on how many comments and how much indentation the file has. The Sample program goes from 720 to 261 bytes, but code with few comments shrinks much less. The badge above the output shows the real numbers for your file.
What happens if my code has a mistake?
Problems that stop the code from being read, such as an unclosed string, comment or bracket, a bracket that does not match, or a stray character, are reported with the line and column and no output is shown. The minifier does not type-check, so code that reads fine but would not compile is minified as it is.
Should I ship minified Rust source?
Usually not. rustc produces the same binary whether or not your source has comments and spacing, so minifying does not make programs smaller or faster. It is handy for pasting code where space is tight, for code golf, or for checking how much of a file is comments.
Is my code uploaded anywhere?
No. The minifying runs in your browser and the code is not sent to our server. Nothing is kept either: a reload starts with an empty editor.
Related Tools
Rust Formatter
Format and beautify Rust code with proper indentation
Rust Validator
Validate Rust syntax and catch common errors
Cargo.toml Generator
Generate Cargo.toml files for Rust projects with dependencies
Cargo.toml Validator
Validate Cargo.toml manifest files for Rust projects
Rust Test Generator
Generate unit test templates for Rust functions
XML Minifier
Minify and compress XML data