Loading…

About Online Rust Formatter

Paste your Rust code, adjust indent size or line length, and hit "Format" to instantly get idiomatic, rustfmt-style output. Perfect for Rustaceans working on crates, APIs, microservices, CLIs or learning projects. No signup, no configuration hell β€” just clean Rust.

Why Use This Rust Formatter

  • rustfmt-style formatting for idiomatic Rust code
  • Adjustable indent size and maximum line width (wrap column)
  • Idempotent formatting – reformatting the same file yields the same result
  • Layout-only changes: indentation, spacing and line breaks, not logic
  • Works great with standalone `.rs` files and crate modules
  • Code processed via a secure formatting backend – no public sharing or indexing
  • Responsive interface that plays nicely with dark/light editor themes

πŸ› οΈ How to Format Rust Code Online for rust-formatter

1

1. Paste or Upload Your Rust Code

πŸ“₯ Paste your Rust code into the editor or drag-and-drop a `.rs` file from your project. The tool detects Rust syntax and highlights it for easier inspection.

2

2. Adjust Indent & Line Length

πŸ“ Use the options panel to pick your **Indent Size** (e.g. 2 or 4 spaces) and **Wrap Line Length** (for long chains or complex expressions). Set wrapping to `0` if you prefer to keep existing line widths.

3

3. Click "Format"

πŸš€ Hit the **Format** button. Your code is sent to a rustfmt-style formatter, which normalizes indentation, spaces, blank lines and wrapping without changing behavior.

4

4. Review, Copy or Download

πŸ“€ Compare before/after, then copy the formatted code back into your editor or download the result as a `.rs` file. Ready for `cargo build`, `cargo test` and code review.

Technical Specifications

Formatting Engine & Style

The formatter follows rustfmt-style, opinionated formatting, so your code looks like idiomatic Rust across files and contributors.

AspectBehaviorNotes
IndentationConfigurable, typically 2–4 spaces per levelTabs are normalized to spaces to match common Rust style in many projects.
Braces & BlocksConsistent placement and indentation for `fn`, `impl`, `match`, `if`, `loop`…Helps keep nested control-flow and match expressions readable.
SpacingCleans up extra spaces around operators and punctuationStandardizes `let`, `match`, closures and generics spacing.
Blank linesNormalized between items (functions, structs, impl blocks)Improves visual separation of modules and API surfaces.
IdempotencySame input β†’ same output when already formattedRe-running the formatter is always safe and stable.

Indent Size & Line Wrapping

You can tailor indentation width and wrapping to match your team’s conventions.

SettingValue RangeEffect
indentSize1–8 spacesControls how much indentation each nested block adds.
wrapLineLength = 0No line-length-based wrappingKeeps your existing line width (useful for quick touch-ups).
wrapLineLength = 80–100Typical Rust team preferencesKeeps code compact while staying highly readable.
wrapLineLength = 101–120Looser layoutGood for ultra-wide monitors or exploratory code.

Supported Input & Limits

Built for everyday Rust development workflows.

ParameterLimit / BehaviorNotes
File extensions.rsSource files for crates, binaries, modules and examples.
MIME typestext/x-rustsrcUsed internally for syntax highlighting and editor mode.
Max input sizeβ‰ˆ 2 MB of Rust sourceExtremely large or generated files are better handled via `cargo fmt` on your machine.
EncodingUTF-8 recommendedNon-UTF-8 inputs should be converted before formatting.

Execution & Safety

Formatting is executed on a secured backend using a Rust-aware formatter.

AspectBehaviorNotes
TransportHTTPS requests to the formatting APIYour code is sent securely and not exposed publicly.
Timeout~25 seconds per requestPrevents runaway jobs on extreme or malformed inputs.
SemanticsLayout-only transformationsYour logic stays the same; only whitespace and layout are changed.

rustfmt & cargo fmt CLI Examples

Want the same style directly in your editor, CI or terminal? Use `rustfmt` or `cargo fmt` locally:

Cross-platform (via Rust toolchain)

Format the entire crate

cargo fmt

Runs rustfmt on all `.rs` files in your crate according to `rustfmt.toml`.

Format a single file

rustfmt src/main.rs

Applies rustfmt rules to one file in place.

Use a custom max width

rustfmt --config max_width=100 src/lib.rs

Overrides the default wrapping column (similar to this tool’s wrap line length).

Add `cargo fmt` to your CI and pre-commit hooks so every commit stays consistently formatted across the team.

Common Use Cases for the Rust Formatter

Crate & Library Development

Keep your public crates, internal libraries and microservices clean and consistent.

  • Normalize formatting before publishing to crates.io.
  • Make contributions from multiple developers visually consistent.
  • Run big refactors and then auto-format to clean up layout.
pub fn add(a: i32,b:i32)->i32{a+b}
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

CLI Tools & Automation

Format concise, maintainable CLI tools, scripts and dev utilities.

  • Keep argument parsing and error handling blocks readable.
  • Clean up long `match` chains and options handling.
  • Ensure internal tooling stays easy to extend over time.

Learning & Teaching Rust

Show students idiomatic Rust without bikeshedding style.

  • Format examples before sharing in slides or tutorials.
  • Compare messy vs. formatted Rust to teach best practices.
  • Auto-format student submissions before reviewing logic.
fn fibonacci(n:u32)->u32{if n<2{n}else{fibonacci(n-1)+fibonacci(n-2)}}
fn fibonacci(n: u32) -> u32 {
    if n < 2 {
        n
    } else {
        fibonacci(n - 1) + fibonacci(n - 2)
    }
}

❓ Frequently Asked Questions

❓Will this Rust formatter change how my code behaves?

The goal is to only change layoutβ€”indentation, spacing and line breaksβ€”while leaving the semantics of valid Rust code intact. As always, you should still run your tests after large formatting passes, especially in critical codebases.

πŸ“How should I choose the wrap line length?

Many Rust teams use 80 or 100 characters as a good balance between readability and compactness. You can pick any value between 0 and 120 in this tool. The most important part is to pick one value and use it consistently across your project.

🧹Does the formatter remove trailing whitespace?

Yes, typical rustfmt-style formatting removes trailing whitespace and normalizes indentation. This keeps your diffs clean and avoids unnecessary noise in pull requests.

πŸ”’Is it safe to format production Rust code here?

Your code is sent securely to a formatting backend and is not published or indexed. That said, for highly sensitive or proprietary systems, you may prefer running `rustfmt` or `cargo fmt` on your own infrastructure.

βš™οΈHow does this relate to rustfmt and cargo fmt?

This online formatter aims to match rustfmt’s behavior so you can prototype formatting and then rely on `cargo fmt` locally or in CI for your full codebase. Align your `rustfmt.toml` settings with the options you use here for best results.

Pro Tips

Best Practice

Add a `rustfmt.toml` at the root of your crate so local `cargo fmt` and this online formatter stay aligned on width and indentation.

CI Tip

Combine this tool with a CI job that runs `cargo fmt -- --check` to prevent unformatted code from landing on your main branch.

Best Practice

Run a dedicated formatting commit (or PR) before big refactors so future diffs focus on real logic changes.

Best Practice

Use shorter line lengths (80–90) for teaching and documentation; go a bit wider in internal tools if your team prefers it.

Additional Resources

Other Tools