rustfmt
rustfmt, Rust's own formatter.
- Formats
- Rust
- In
ur.toml [format.rust] style = "rustfmt"- Its options
- https://rust-lang.github.io/rustfmt/
Formatted
cart.rs, as written// What a cart costs.
fn subtotal(items:&[Item])->u64{items.iter().map(|item|item.price*item.quantity).sum()}
fn label(item:&Item)->String{item.name.clone()+" x"+&item.quantity.to_string()}
ur format --style rustfmt cart.rs// What a cart costs.
fn subtotal(items: &[Item]) -> u64 {
items.iter().map(|item| item.price * item.quantity).sum()
}
fn label(item: &Item) -> String {
item.name.clone() + " x" + &item.quantity.to_string()
}
Defaults
The shared options, as rustfmt sets them when nothing configures it. Its own names for them are read from its configuration files, but ur.toml sets them by the shared name only.
| Option | Default | rustfmt calls it |
|---|---|---|
width | 100 | max_width |
indent.width | 4 | tab_spaces |
indent.style | hard_tabs | |
line-ending | "auto" | newline_style |
trailing-comma.add | "multiline" | |
trailing-comma.forces-break | false | |
quotes | "preserve" | |
target | edition |
Its own options
Set in a table named for it, such as [format.rustfmt], or in a language's table, as rustfmt = { ... }.
| Option | Default | What it does |
|---|---|---|
use-small-heuristics | "Default" | How the widths below are set when not set themselves: as fractions of the line width (Default), not at all (Off), or to the whole line width (Max). Takes "Default", "Off", "Max". |
fn-call-width | 60 | The widest a function call's arguments can be before they break across lines. Takes a number. |
attr-fn-like-width | 70 | The widest a function-like attribute's arguments can be before they break across lines. Takes a number. |
struct-lit-width | 18 | The widest a struct literal's body can be before it breaks across lines. Takes a number. |
struct-variant-width | 35 | The widest a struct variant's body can be before it breaks across lines. Takes a number. |
array-width | 60 | The widest an array literal can be before it breaks across lines. Takes a number. |
chain-width | 60 | The widest a chain of method calls can be before it breaks, a call to a line. Takes a number. |
single-line-if-else-max-width | 50 | The widest an if-else expression can be and stay on one line. Takes a number. |
single-line-let-else-max-width | 50 | The widest a let-else statement's else block can be and stay on one line. Takes a number. |
reorder-imports | true | Sort each group of consecutive use declarations. Takes true or false. |
reorder-modules | true | Sort each group of consecutive mod declarations. Takes true or false. |
remove-nested-parens | true | Remove parentheses wrapped directly in other parentheses. Takes true or false. |
short-array-element-width-threshold | 10 | The widest an array's elements can be for several to share a line when the array breaks. Takes a number. |
match-arm-leading-pipes | "Never" | Whether each match arm begins with a |: Always, Never, or as written (Preserve). Takes "Always", "Never", "Preserve". |
fn-params-layout | "Tall" | How a function's parameters break: as many to a line as fit (Compressed), all on one line or one to a line (Tall), or always one to a line (Vertical). Takes "Compressed", "Tall", "Vertical". |
match-block-trailing-comma | false | Put a comma after a match arm whose body is a block. Takes true or false. |
style-edition | | The Rust style edition to format in, which otherwise follows the code's edition. Takes "2015", "2018", "2021", "2024", "2027". |
merge-derives | true | Merge an item's #[derive(...)] attributes into one. Takes true or false. |
use-try-shorthand | false | Write expr? in place of try!(expr). Takes true or false. |
use-field-init-shorthand | false | Write S { x } in place of S { x: x }. Takes true or false. |
force-explicit-abi | true | Write extern "C" where extern alone is written. Takes true or false. |
disable-all-formatting | false | Leave every file as written. Takes true or false. |