How to use the Rust Formatter
Format Rust code in three steps:
1
Paste or upload Rust code
Paste Rust source code into the input panel or upload a .rs file. Any indentation style is accepted as input.
2
Choose indent size
Select 2 or 4 spaces. rustfmt defaults to 4 spaces, but select 2 if your project's rustfmt.toml specifies otherwise.
3
Format, copy, or download
Click 'Format Rust' to apply rustfmt-style indentation and Rust syntax highlighting. Copy the result or download as a .rs file.
When to use this tool
Use the Rust formatter when you need clean, rustfmt-consistent code without running Cargo locally:
- →Formatting Rust code snippets from The Rust Book, Rustonomicon, or crate documentation
- →Cleaning up Rust code generated by AI assistants before integrating into a Cargo project
- →Preparing Rust examples for technical blog posts, conference talks, or crate README files
- →Quickly reformatting a Rust struct, enum, or trait implementation for a code review comment
- →Checking how Rust code will look after rustfmt on a machine without Rust installed
- →Formatting Rust interview or coding challenge solutions for readable submission
Frequently asked questions
Q:Does this formatter replicate what running 'cargo fmt' does?
This formatter applies rustfmt's block indentation logic and handles Rust's unique constructs accurately for most practical code. Full cargo fmt additionally manages trailing commas in function arguments and match arms, blank lines between items, import grouping and merging (use std::{io, fmt}), and complex expression formatting — these require full Rust AST parsing and are beyond a browser-based formatter. Use this for quick formatting; use cargo fmt in your project pipeline for complete compliance.
Q:How are Rust lifetime annotations displayed?
Lifetime annotations ('a, 'static, 'lifetime) are recognised and highlighted in italic orange in the syntax output, making them visually distinct from type names and keywords. This is particularly helpful when reading complex Rust signatures with multiple lifetime parameters.
Q:Does it handle macros like println!, vec!, format!, and custom macros?
Yes — macro invocations (identifier followed by !) are detected and highlighted in the syntax output in green. The formatter preserves macro call content as-is and indents the macro invocation line according to its surrounding block level.
Q:Can it format Rust attributes like #[derive(Debug, Clone)] and #[cfg(test)]?
Yes — outer attributes (#[...]) and inner attributes (#![...]) are detected and kept at the same indentation level as the item they apply to. They are highlighted in orange in the syntax output for easy identification.
Q:Does it work with async Rust and tokio/async-std patterns?
Yes — async, await, dyn, impl Trait in return position, and Pin are recognised Rust keywords and are correctly handled in indentation and highlighted. Async function bodies and .await chains are indented as normal Rust expressions.
Q:What Rust standard library types are highlighted?
Commonly used standard library types including Vec, String, HashMap, HashSet, Option, Result, Box, Rc, Arc, Cell, RefCell, Mutex, RwLock, BTreeMap, and BTreeSet are highlighted in blue to distinguish them from user-defined types. Primitive types (i32, u64, f64, bool, str, char, usize) are also highlighted.