🔧 Programming
✅ 100% Free
⚡ Instant
Rust Formatter
Format Rust code with rustfmt-style 4-space indentation. Handles fn, impl, struct, enum, match arms, and trait definitions.
📥 Input
📤 Output
Ready — paste code and click Format · Ctrl+Enter to format
Lines: —
Size: —
Load example:
▶ Struct
▶ Match
What is rustfmt?
rustfmt is Rust's official code formatting tool, installed with Rustup and run via cargo fmt. Like Go's gofmt, rustfmt enforces a single canonical style for all Rust code: 4-space indentation, K&R-style braces, and specific rules for match arms, generics, and trait bounds. Following rustfmt conventions is essential for contributing to Rust open-source projects.
Frequently Asked Questions
Rust's ownership model ensures memory safety without a garbage collector. Every value has a single owner, and the value is dropped when the owner goes out of scope. Values can be borrowed (as references
&T or mutable references &mut T), with the compiler enforcing that mutable borrows are exclusive. This eliminates data races and use-after-free bugs at compile time.String is an owned, heap-allocated, mutable string. &str is a string slice — a borrowed reference to a sequence of UTF-8 bytes, often a view into a String or a string literal. Use &str for function parameters (more flexible), and String when you need to own and mutate the string.Option represents a value that may or may not exist: Some(value) or None. Rust has no null — any type that could be absent must be wrapped in Option, forcing you to handle the absence case explicitly. This eliminates null pointer dereferences, one of the most common sources of bugs in other languages.
Done!