Rustfmt is designed to be very configurable. You can create a TOML file called rustfmt.toml
or .rustfmt.toml
, place it in the project or any other parent directory and it will apply the options in that file.
A possible content of rustfmt.toml
or .rustfmt.toml
might look like this:
indent_style = "Block"
reorder_imports = false
Each configuration option is either stable or unstable.
Stable options can be used directly, while unstable options are opt-in.
To enable unstable options, set unstable_features = true
in rustfmt.toml
or pass --unstable-features
to rustfmt.
As mentioned above, rustfmt searches for a rustfmt.toml
or .rustfmt.toml
in the directory it is
run and all parent directories.
If none of these directories contain such a file, both your home directory and a directory called rustfmt
in your global config directory (e.g. .config/rustfmt/
) are checked as well.
If any of these directories contain such a file, rustfmt is configured with the union of configuration options in all such files. The configuration options are applied in order of reverse proximity to the directory rustfmt is run in. For example, consider a directory structure
outer
|-- rustfmt.toml
|-- outer.rs
|-- inner
|-- rustfmt.toml
|-- inner.rs
|-- lib_inner.rs
Where outer/rustfmt.toml
is
max_width = 80
array_width = 50
ignore = ['inner/inner.rs']
and outer/inner/rustfmt.toml
is
max_width = 70
ignore = ['./lib_inner.rs']
If run in the inner
directory, rustfmt's internal configuration will include
max_width = 70
array_width = 50
ignore = ['./lib_inner.rs', './inner.rs']
If run in the root outer
directory, rustfmt's internal configuration will include
max_width = 80
array_width = 50
ignore = ['inner/inner.rs']
Below you can find a detailed visual guide on all the supported configuration options of rustfmt.
Note that the below list reflects the configuration options available on the latest version of rustfmt in source control. Some newer options may not be available yet in a released version of rustfmt.
For version- and channel-specific configurations, please visit https://rust-lang.github.io/rustfmt/.
Maximum width of an array literal before falling back to vertical formatting.
- Default value:
60
- Possible values: any positive integer that is less than or equal to the value specified for
max_width
- Stable: Yes
By default this option is set as a percentage of max_width
provided by width_heuristics
, but a value set directly for array_width
will take precedence.
See also max_width
and width_heuristics
Maximum width of the args of a function-like attributes before falling back to vertical formatting.
- Default value:
70
- Possible values: any positive integer that is less than or equal to the value specified for
max_width
- Stable: Yes
By default this option is set as a percentage of max_width
provided by width_heuristics
, but a value set directly for attr_fn_like_width
will take precedence.
See also max_width
and width_heuristics
Where to put a binary operator when a binary expression goes multiline.
- Default value:
"Front"
- Possible values:
"Front"
,"Back"
- Stable: Yes
fn main() {
let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo
|| barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
let sum = 123456789012345678901234567890
+ 123456789012345678901234567890
+ 123456789012345678901234567890;
let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
}
fn main() {
let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo ||
barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
let sum = 123456789012345678901234567890 +
123456789012345678901234567890 +
123456789012345678901234567890;
let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
}
Minimum number of blank lines which must be put between items. If two items have fewer blank lines between them, additional blank lines are inserted.
- Default value:
0
- Possible values: unsigned integer
- Stable: No (tracking issue: #3382)
Original Code (rustfmt will not change it with the default value of 0
):
#![rustfmt::skip]
fn foo() {
println!("a");
}
fn bar() {
println!("b");
println!("c");
}
fn foo() {
println!("a");
}
fn bar() {
println!("b");
println!("c");
}
Maximum number of blank lines which can be put between items. If more than this number of consecutive empty lines are found, they are trimmed down to match this integer.
- Default value:
1
- Possible values: any non-negative integer
- Stable: No (tracking issue: #3381)
Original Code:
#![rustfmt::skip]
fn foo() {
println!("a");
}
fn bar() {
println!("b");
println!("c");
}
fn foo() {
println!("a");
}
fn bar() {
println!("b");
println!("c");
}
fn foo() {
println!("a");
}
fn bar() {
println!("b");
println!("c");
}
See also: blank_lines_lower_bound
Brace style for items
- Default value:
"SameLineWhere"
- Possible values:
"AlwaysNextLine"
,"PreferSameLine"
,"SameLineWhere"
- Stable: No (tracking issue: #3376)
fn lorem() {
// body
}
fn lorem(ipsum: usize) {
// body
}
fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
fn lorem()
{
// body
}
fn lorem(ipsum: usize)
{
// body
}
fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
fn lorem() {
// body
}
fn lorem(ipsum: usize) {
// body
}
fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div, {
// body
}
struct Lorem {
ipsum: bool,
}
struct Dolor<T>
where
T: Eq,
{
sit: T,
}
struct Lorem
{
ipsum: bool,
}
struct Dolor<T>
where
T: Eq,
{
sit: T,
}
struct Lorem {
ipsum: bool,
}
struct Dolor<T>
where
T: Eq, {
sit: T,
}
Maximum width of a chain to fit on one line.
- Default value:
60
- Possible values: any positive integer that is less than or equal to the value specified for
max_width
- Stable: Yes
By default this option is set as a percentage of max_width
provided by width_heuristics
, but a value set directly for chain_width
will take precedence.
See also max_width
and width_heuristics
Combine control expressions with function calls.
- Default value:
true
- Possible values:
true
,false
- Stable: No (tracking issue: #3369)
fn example() {
// If
foo!(if x {
foo();
} else {
bar();
});
// IfLet
foo!(if let Some(..) = x {
foo();
} else {
bar();
});
// While
foo!(while x {
foo();
bar();
});
// WhileLet
foo!(while let Some(..) = x {
foo();
bar();
});
// ForLoop
foo!(for x in y {
foo();
bar();
});
// Loop
foo!(loop {
foo();
bar();
});
}
fn example() {
// If
foo!(
if x {
foo();
} else {
bar();
}
);
// IfLet
foo!(
if let Some(..) = x {
foo();
} else {
bar();
}
);
// While
foo!(
while x {
foo();
bar();
}
);
// WhileLet
foo!(
while let Some(..) = x {
foo();
bar();
}
);
// ForLoop
foo!(
for x in y {
foo();
bar();
}
);
// Loop
foo!(
loop {
foo();
bar();
}
);
}
Maximum length of comments. No effect unlesswrap_comments = true
.
- Default value:
80
- Possible values: any positive integer
- Stable: No (tracking issue: #3349)
Note: A value of 0
results in wrap_comments
being applied regardless of a line's width.
// Lorem ipsum dolor sit amet, consectetur adipiscing elit.
// Lorem ipsum dolor sit amet,
// consectetur adipiscing elit.
See also wrap_comments
.
Replace strings of _ wildcards by a single .. in tuple patterns
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3384)
fn main() {
let (lorem, ipsum, _, _) = (1, 2, 3, 4);
let (lorem, ipsum, ..) = (1, 2, 3, 4);
}
fn main() {
let (lorem, ipsum, ..) = (1, 2, 3, 4);
}
Brace style for control flow constructs
- Default value:
"AlwaysSameLine"
- Possible values:
"AlwaysNextLine"
,"AlwaysSameLine"
,"ClosingNextLine"
- Stable: No (tracking issue: #3377)
fn main() {
if lorem {
println!("ipsum!");
} else {
println!("dolor!");
}
}
fn main() {
if lorem
{
println!("ipsum!");
}
else
{
println!("dolor!");
}
}
fn main() {
if lorem {
println!("ipsum!");
}
else {
println!("dolor!");
}
}
Specifies which edition is used by the parser.
- Default value:
"2015"
- Possible values:
"2015"
,"2018"
- Stable: Yes
Rustfmt is able to pick up the edition used by reading the Cargo.toml
file if executed
through the Cargo's formatting tool cargo fmt
. Otherwise, the edition needs to be specified
in your config file:
edition = "2018"
Put empty-body functions and impls on a single line
- Default value:
true
- Possible values:
true
,false
- Stable: No (tracking issue: #3356)
fn lorem() {}
impl Lorem {}
fn lorem() {
}
impl Lorem {
}
See also brace_style
, control_brace_style
.
The maximum length of enum variant having discriminant, that gets vertically aligned with others. Variants without discriminants would be ignored for the purpose of alignment.
Note that this is not how much whitespace is inserted, but instead the longest variant name that doesn't get ignored when aligning.
- Default value : 0
- Possible values: any positive integer
- Stable: No (tracking issue: #3372)
enum Bar {
A = 0,
Bb = 1,
RandomLongVariantGoesHere = 10,
Ccc = 71,
}
enum Bar {
VeryLongVariantNameHereA = 0,
VeryLongVariantNameHereBb = 1,
VeryLongVariantNameHereCcc = 2,
}
enum Foo {
A = 0,
Bb = 1,
RandomLongVariantGoesHere = 10,
Ccc = 2,
}
enum Bar {
VeryLongVariantNameHereA = 0,
VeryLongVariantNameHereBb = 1,
VeryLongVariantNameHereCcc = 2,
}
Error if Rustfmt is unable to get all lines within max_width
, except for comments and string
literals. If this happens, then it is a bug in Rustfmt. You might be able to work around the bug by
refactoring your code to avoid long/complex expressions, usually by extracting a local variable or
using a shorter name.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3391)
See also max_width
.
Error if unable to get comments or string literals within max_width
, or they are left with
trailing whitespaces.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3392)
Control the layout of parameters in a function signature
- Default value:
"Tall"
- Possible values:
"Compressed"
,"Tall"
,"Vertical"
- Stable: Yes
trait Lorem {
fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
// body
}
fn lorem(
ipsum: Ipsum,
dolor: Dolor,
sit: Sit,
amet: Amet,
consectetur: Consectetur,
adipiscing: Adipiscing,
elit: Elit,
);
fn lorem(
ipsum: Ipsum,
dolor: Dolor,
sit: Sit,
amet: Amet,
consectetur: Consectetur,
adipiscing: Adipiscing,
elit: Elit,
) {
// body
}
}
trait Lorem {
fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
// body
}
fn lorem(
ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
adipiscing: Adipiscing, elit: Elit,
);
fn lorem(
ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
adipiscing: Adipiscing, elit: Elit,
) {
// body
}
}
trait Lorem {
fn lorem(
ipsum: Ipsum,
dolor: Dolor,
sit: Sit,
amet: Amet,
);
fn lorem(
ipsum: Ipsum,
dolor: Dolor,
sit: Sit,
amet: Amet,
) {
// body
}
fn lorem(
ipsum: Ipsum,
dolor: Dolor,
sit: Sit,
amet: Amet,
consectetur: Consectetur,
adipiscing: Adipiscing,
elit: Elit,
);
fn lorem(
ipsum: Ipsum,
dolor: Dolor,
sit: Sit,
amet: Amet,
consectetur: Consectetur,
adipiscing: Adipiscing,
elit: Elit,
) {
// body
}
}
Maximum width of the args of a function call before falling back to vertical formatting.
- Default value:
60
- Possible values: any positive integer that is less than or equal to the value specified for
max_width
- Stable: Yes
By default this option is set as a percentage of max_width
provided by width_heuristics
, but a value set directly for fn_call_width
will take precedence.
See also max_width
and width_heuristics
Put single-expression functions on a single line
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3358)
fn lorem() -> usize {
42
}
fn lorem() -> usize {
let ipsum = 42;
ipsum
}
fn lorem() -> usize { 42 }
fn lorem() -> usize {
let ipsum = 42;
ipsum
}
See also control_brace_style
.
Always print the abi for extern items
- Default value:
true
- Possible values:
true
,false
- Stable: Yes
Note: Non-"C" ABIs are always printed. If false
then "C" is removed.
extern "C" {
pub static lorem: c_int;
}
extern {
pub static lorem: c_int;
}
Force multiline closure and match arm bodies to be wrapped in a block
- Default value:
false
- Possible values:
false
,true
- Stable: No (tracking issue: #3374)
fn main() {
result.and_then(|maybe_value| match maybe_value {
None => foo(),
Some(value) => bar(),
});
match lorem {
None => |ipsum| {
println!("Hello World");
},
Some(dolor) => foo(),
}
}
fn main() {
result.and_then(|maybe_value| {
match maybe_value {
None => foo(),
Some(value) => bar(),
}
});
match lorem {
None => {
|ipsum| {
println!("Hello World");
}
}
Some(dolor) => foo(),
}
}
Format code snippet included in doc comments.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3348)
/// Adds one to the number given.
///
/// # Examples
///
/// ```rust
/// let five=5;
///
/// assert_eq!(
/// 6,
/// add_one(5)
/// );
/// # fn add_one(x: i32) -> i32 {
/// # x + 1
/// # }
/// ```
fn add_one(x: i32) -> i32 {
x + 1
}
/// Adds one to the number given.
///
/// # Examples
///
/// ```rust
/// let five = 5;
///
/// assert_eq!(6, add_one(5));
/// # fn add_one(x: i32) -> i32 {
/// # x + 1
/// # }
/// ```
fn add_one(x: i32) -> i32 {
x + 1
}
Format generated files. A file is considered as generated if the file starts with // @generated
.
- Default value:
false
- Possible values:
true
,false
- Stable: No
Format the metavariable matching patterns in macros.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3354)
macro_rules! foo {
($a: ident : $b: ty) => {
$a(42): $b;
};
($a: ident $b: ident $c: ident) => {
$a = $b + $c;
};
}
macro_rules! foo {
($a:ident : $b:ty) => {
$a(42): $b;
};
($a:ident $b:ident $c:ident) => {
$a = $b + $c;
};
}
See also format_macro_bodies
.
Format the bodies of macros.
- Default value:
true
- Possible values:
true
,false
- Stable: No (tracking issue: #3355)
macro_rules! foo {
($a: ident : $b: ty) => {
$a(42): $b;
};
($a: ident $b: ident $c: ident) => {
$a = $b + $c;
};
}
macro_rules! foo {
($a: ident : $b: ty) => { $a(42): $b; };
($a: ident $b: ident $c: ident) => { $a=$b+$c; };
}
See also format_macro_matchers
.
Format string literals where necessary
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3353)
fn main() {
let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit amet consectetur adipiscing";
}
fn main() {
let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit amet \
consectetur adipiscing";
}
See also max_width
.
Use tab characters for indentation, spaces for alignment
- Default value:
false
- Possible values:
true
,false
- Stable: Yes
fn lorem() -> usize {
42 // spaces before 42
}
fn lorem() -> usize {
42 // tabs before 42
}
See also: tab_spaces
.
Do not show parse errors if the parser failed to parse files.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3390)
Skip formatting files and directories that match the specified pattern.
The pattern format is the same as .gitignore. Be sure to use Unix/forwardslash /
style paths. This path style will work on all platforms. Windows style paths with backslashes \
are not supported.
- Default value: format every file
- Possible values: See an example below
- Stable: Yes
If you want to ignore specific files, put the following to your config file:
ignore = [
"src/types.rs",
"src/foo/bar.rs",
]
If you want to ignore every file under examples/
, put the following to your config file:
ignore = [
"examples",
]
If you want to ignore every file under the directory where you put your rustfmt.toml:
ignore = ["/"]
Indent style of imports
- Default Value:
"Block"
- Possible values:
"Block"
,"Visual"
- Stable: No (tracking issue: #3360)
use foo::{
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
};
use foo::{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz};
See also: imports_layout
.
Item layout inside a imports block
- Default value: "Mixed"
- Possible values: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
- Stable: No (tracking issue: #3361)
use foo::{xxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz};
use foo::{
aaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbb, cccccccccccccccccc, dddddddddddddddddd,
eeeeeeeeeeeeeeeeee, ffffffffffffffffff,
};
Note: This option forces all imports onto one line and may exceed max_width
.
use foo::{xxx, yyy, zzz};
use foo::{aaa, bbb, ccc, ddd, eee, fff};
use foo::{xxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz};
use foo::{
aaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbb,
cccccccccccccccccc,
dddddddddddddddddd,
eeeeeeeeeeeeeeeeee,
ffffffffffffffffff,
};
use foo::{
xxx,
yyy,
zzz,
};
use foo::{
aaa,
bbb,
ccc,
ddd,
eee,
fff,
};
Indent on expressions or items.
- Default value:
"Block"
- Possible values:
"Block"
,"Visual"
- Stable: No (tracking issue: #3346)
fn main() {
let lorem = vec![
"ipsum",
"dolor",
"sit",
"amet",
"consectetur",
"adipiscing",
"elit",
];
}
fn main() {
let lorem = vec!["ipsum",
"dolor",
"sit",
"amet",
"consectetur",
"adipiscing",
"elit"];
}
fn main() {
if lorem_ipsum
&& dolor_sit
&& amet_consectetur
&& lorem_sit
&& dolor_consectetur
&& amet_ipsum
&& lorem_consectetur
{
// ...
}
}
fn main() {
if lorem_ipsum
&& dolor_sit
&& amet_consectetur
&& lorem_sit
&& dolor_consectetur
&& amet_ipsum
&& lorem_consectetur
{
// ...
}
}
See also: control_brace_style
.
fn lorem() {}
fn lorem(ipsum: usize) {}
fn lorem(
ipsum: usize,
dolor: usize,
sit: usize,
amet: usize,
consectetur: usize,
adipiscing: usize,
elit: usize,
) {
// body
}
fn lorem() {}
fn lorem(ipsum: usize) {}
fn lorem(ipsum: usize,
dolor: usize,
sit: usize,
amet: usize,
consectetur: usize,
adipiscing: usize,
elit: usize) {
// body
}
fn main() {
lorem(
"lorem",
"ipsum",
"dolor",
"sit",
"amet",
"consectetur",
"adipiscing",
"elit",
);
}
fn main() {
lorem("lorem",
"ipsum",
"dolor",
"sit",
"amet",
"consectetur",
"adipiscing",
"elit");
}
fn lorem<
Ipsum: Eq = usize,
Dolor: Eq = usize,
Sit: Eq = usize,
Amet: Eq = usize,
Adipiscing: Eq = usize,
Consectetur: Eq = usize,
Elit: Eq = usize,
>(
ipsum: Ipsum,
dolor: Dolor,
sit: Sit,
amet: Amet,
adipiscing: Adipiscing,
consectetur: Consectetur,
elit: Elit,
) -> T {
// body
}
fn lorem<Ipsum: Eq = usize,
Dolor: Eq = usize,
Sit: Eq = usize,
Amet: Eq = usize,
Adipiscing: Eq = usize,
Consectetur: Eq = usize,
Elit: Eq = usize>(
ipsum: Ipsum,
dolor: Dolor,
sit: Sit,
amet: Amet,
adipiscing: Adipiscing,
consectetur: Consectetur,
elit: Elit)
-> T {
// body
}
fn main() {
let lorem = Lorem {
ipsum: dolor,
sit: amet,
};
}
fn main() {
let lorem = Lorem { ipsum: dolor,
sit: amet };
}
See also: struct_lit_single_line
, indent_style
.
fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
where
Ipsum: Eq,
Dolor: Eq,
Sit: Eq,
Amet: Eq,
{
// body
}
fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
where Ipsum: Eq,
Dolor: Eq,
Sit: Eq,
Amet: Eq
{
// body
}
Write an item and its attribute on the same line if their combined width is below a threshold
- Default value: 0
- Possible values: any positive integer
- Stable: No (tracking issue: #3343)
#[cfg(feature = "alloc")]
use core::slice;
#[cfg(feature = "alloc")] use core::slice;
Check whether beginnings of files match a license template.
- Default value:
""
- Possible values: path to a license template file
- Stable: No (tracking issue: #3352)
A license template is a plain text file which is matched literally against the
beginning of each source file, except for {}
-delimited blocks, which are
matched as regular expressions. The following license template therefore
matches strings like // Copyright 2017 The Rust Project Developers.
, // Copyright 2018 The Rust Project Developers.
, etc.:
// Copyright {\d+} The Rust Project Developers.
\{
, \}
and \\
match literal braces / backslashes.
Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
- Default value:
true
- Possible values:
true
,false
- Stable: No (tracking issue: #3373)
fn main() {
match lorem {
true => {
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
}
false => println!("{}", sit),
}
}
fn main() {
match lorem {
true =>
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
false => println!("{}", sit),
}
}
See also: match_block_trailing_comma
.
Controls whether to include a leading pipe on match arms
- Default value:
Never
- Possible values:
Always
,Never
,Preserve
- Stable: Yes
// Leading pipes are from this:
// fn foo() {
// match foo {
// | "foo" | "bar" => {}
// | "baz"
// | "something relatively long"
// | "something really really really realllllllllllllly long" => println!("x"),
// | "qux" => println!("y"),
// _ => {}
// }
// }
// Are removed:
fn foo() {
match foo {
"foo" | "bar" => {}
"baz"
| "something relatively long"
| "something really really really realllllllllllllly long" => println!("x"),
"qux" => println!("y"),
_ => {}
}
}
// Leading pipes are emitted on all arms of this:
// fn foo() {
// match foo {
// "foo" | "bar" => {}
// "baz"
// | "something relatively long"
// | "something really really really realllllllllllllly long" => println!("x"),
// "qux" => println!("y"),
// _ => {}
// }
// }
// Becomes:
fn foo() {
match foo {
| "foo" | "bar" => {}
| "baz"
| "something relatively long"
| "something really really really realllllllllllllly long" => println!("x"),
| "qux" => println!("y"),
| _ => {}
}
}
fn foo() {
match foo {
| "foo" | "bar" => {}
| "baz"
| "something relatively long"
| "something really really really realllllllllllllly long" => println!("x"),
| "qux" => println!("y"),
_ => {}
}
match baz {
"qux" => {}
"foo" | "bar" => {}
_ => {}
}
}
Put a trailing comma after a block based match arm (non-block arms are not affected)
- Default value:
false
- Possible values:
true
,false
- Stable: Yes
fn main() {
match lorem {
Lorem::Ipsum => {
println!("ipsum");
}
Lorem::Dolor => println!("dolor"),
}
}
fn main() {
match lorem {
Lorem::Ipsum => {
println!("ipsum");
},
Lorem::Dolor => println!("dolor"),
}
}
See also: trailing_comma
, match_arm_blocks
.
Maximum width of each line
- Default value:
100
- Possible values: any positive integer
- Stable: Yes
See also error_on_line_overflow
.
Merge multiple derives into a single one.
- Default value:
true
- Possible values:
true
,false
- Stable: Yes
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
pub enum Foo {}
#[derive(Eq, PartialEq)]
#[derive(Debug)]
#[derive(Copy, Clone)]
pub enum Foo {}
Merge multiple imports into a single nested import.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3362)
use foo::{a, c, d};
use foo::{b, g};
use foo::{e, f};
use foo::{a, b, c, d, e, f, g};
Unix or Windows line endings
- Default value:
"Auto"
- Possible values:
"Auto"
,"Native"
,"Unix"
,"Windows"
- Stable: Yes
The newline style is detected automatically on a per-file basis. Files with mixed line endings will be converted to the first detected line ending style.
Line endings will be converted to \r\n
on Windows and \n
on all
other platforms.
Line endings will be converted to \n
.
Line endings will be converted to \r\n
.
Convert /* */ comments to // comments where possible
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3350)
// Lorem ipsum:
fn dolor() -> usize {}
/* sit amet: */
fn adipiscing() -> usize {}
// Lorem ipsum:
fn dolor() -> usize {}
// sit amet:
fn adipiscing() -> usize {}
Convert #![doc]
and #[doc]
attributes to //!
and ///
doc comments.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3351)
#![doc = "Example documentation"]
#[doc = "Example item documentation"]
pub enum Foo {}
//! Example documentation
/// Example item documentation
pub enum Foo {}
Preserves blanks lines at the start of the block. Note that this will preserve newlines, but strip any other whitespace on those lines.
- Default value:
false
- Possible values:
true
,false
- Stable: No
fn say_hi() {
println!("hi");
}
fn say_hi() {
println!("hi");
}
When structs, slices, arrays, and block/array-like macros are used as the last argument in an expression list, allow them to overflow (like blocks/closures) instead of being indented on a new line.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3370)
fn example() {
foo(ctx, |param| {
action();
foo(param)
});
foo(
ctx,
Bar {
x: value,
y: value2,
},
);
foo(
ctx,
&[
MAROON_TOMATOES,
PURPLE_POTATOES,
ORGANE_ORANGES,
GREEN_PEARS,
RED_APPLES,
],
);
foo(
ctx,
vec![
MAROON_TOMATOES,
PURPLE_POTATOES,
ORGANE_ORANGES,
GREEN_PEARS,
RED_APPLES,
],
);
}
fn example() {
foo(ctx, |param| {
action();
foo(param)
});
foo(ctx, Bar {
x: value,
y: value2,
});
foo(ctx, &[
MAROON_TOMATOES,
PURPLE_POTATOES,
ORGANE_ORANGES,
GREEN_PEARS,
RED_APPLES,
]);
foo(ctx, vec![
MAROON_TOMATOES,
PURPLE_POTATOES,
ORGANE_ORANGES,
GREEN_PEARS,
RED_APPLES,
]);
}
Remove nested parens.
- Default value:
true
, - Possible values:
true
,false
- Stable: Yes
fn main() {
(foo());
}
fn main() {
((((foo()))));
}
Reorder impl items. type
and const
are put first, then macros and methods.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3363)
struct Dummy;
impl Iterator for Dummy {
fn next(&mut self) -> Option<Self::Item> {
None
}
type Item = i32;
}
struct Dummy;
impl Iterator for Dummy {
type Item = i32;
fn next(&mut self) -> Option<Self::Item> {
None
}
}
Reorder import and extern crate statements alphabetically in groups (a group is separated by a newline).
- Default value:
true
- Possible values:
true
,false
- Stable: Yes
use dolor;
use ipsum;
use lorem;
use sit;
use lorem;
use ipsum;
use dolor;
use sit;
Reorder mod
declarations alphabetically in group.
- Default value:
true
- Possible values:
true
,false
- Stable: Yes
mod a;
mod b;
mod dolor;
mod ipsum;
mod lorem;
mod sit;
mod b;
mod a;
mod lorem;
mod ipsum;
mod dolor;
mod sit;
Note mod
with #[macro_export]
will not be reordered since that could change the semantics
of the original source code.
Require a specific version of rustfmt. If you want to make sure that the specific version of rustfmt is used in your CI, use this option.
- Default value:
CARGO_PKG_VERSION
- Possible values: any published version (e.g.
"0.3.8"
) - Stable: No (tracking issue: #3386)
Maximum line length for single line if-else expressions. A value of 0
(zero) results in if-else expressions always being broken into multiple lines. Note this occurs when width_heuristics
is set to Off
.
- Default value:
50
- Possible values: any positive integer that is less than or equal to the value specified for
max_width
- Stable: Yes
By default this option is set as a percentage of max_width
provided by width_heuristics
, but a value set directly for single_line_if_else_max_width
will take precedence.
See also max_width
and width_heuristics
Leave a space after the colon.
- Default value:
true
- Possible values:
true
,false
- Stable: No (tracking issue: #3366)
fn lorem<T: Eq>(t: T) {
let lorem: Dolor = Lorem {
ipsum: dolor,
sit: amet,
};
}
fn lorem<T:Eq>(t:T) {
let lorem:Dolor = Lorem {
ipsum:dolor,
sit:amet,
};
}
See also: space_before_colon
.
Leave a space before the colon.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3365)
fn lorem<T: Eq>(t: T) {
let lorem: Dolor = Lorem {
ipsum: dolor,
sit: amet,
};
}
fn lorem<T : Eq>(t : T) {
let lorem : Dolor = Lorem {
ipsum : dolor,
sit : amet,
};
}
See also: space_after_colon
.
Whether to put a space before the opening paren in function signatures
- Default value:
false
- Possible values:
true
,false
- Stable: No
fn lorem() {
// body
}
fn lorem(ipsum: usize) {
// body
}
fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
fn lorem () {
// body
}
fn lorem (ipsum: usize) {
// body
}
fn lorem<T> (ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
Put spaces around the .., ..=, and ... range operators
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3367)
fn main() {
let lorem = 0..10;
let ipsum = 0..=10;
match lorem {
1..5 => foo(),
_ => bar,
}
match lorem {
1..=5 => foo(),
_ => bar,
}
match lorem {
1...5 => foo(),
_ => bar,
}
}
fn main() {
let lorem = 0 .. 10;
let ipsum = 0 ..= 10;
match lorem {
1 .. 5 => foo(),
_ => bar,
}
match lorem {
1 ..= 5 => foo(),
_ => bar,
}
match lorem {
1 ... 5 => foo(),
_ => bar,
}
}
Determines if '=' are wrapped in spaces in attributes.
- Default value:
true
- Possible values:
true
,false
- Stable: No
#[cfg(not(target_os = "pi"))]
println!("os is not pi!");
#[cfg(not(target_os="pi"))]
println!("os is not pi!");
The maximum diff of width between struct fields to be aligned with each other.
- Default value : 0
- Possible values: any non-negative integer
- Stable: No (tracking issue: #3371)
struct Foo {
x: u32,
yy: u32,
zzz: u32,
}
struct Foo {
x: u32,
yy: u32,
zzz: u32,
}
Put small struct literals on a single line
- Default value:
true
- Possible values:
true
,false
- Stable: No (tracking issue: #3357)
fn main() {
let lorem = Lorem { foo: bar, baz: ofo };
}
fn main() {
let lorem = Lorem {
foo: bar,
baz: ofo,
};
}
See also: indent_style
.
Maximum width in the body of a struct literal before falling back to vertical formatting. A value of 0
(zero) results in struct literals always being broken into multiple lines. Note this occurs when width_heuristics
is set to Off
.
- Default value:
18
- Possible values: any positive integer that is less than or equal to the value specified for
max_width
- Stable: Yes
By default this option is set as a percentage of max_width
provided by width_heuristics
, but a value set directly for struct_lit_width
will take precedence.
See also max_width
, width_heuristics
, and struct_lit_single_line
Maximum width in the body of a struct variant before falling back to vertical formatting. A value of 0
(zero) results in struct literals always being broken into multiple lines. Note this occurs when width_heuristics
is set to Off
.
- Default value:
35
- Possible values: any positive integer that is less than or equal to the value specified for
max_width
- Stable: Yes
By default this option is set as a percentage of max_width
provided by width_heuristics
, but a value set directly for struct_variant_width
will take precedence.
See also max_width
and width_heuristics
Number of spaces per tab
- Default value:
4
- Possible values: any positive integer
- Stable: Yes
fn lorem() {
let ipsum = dolor();
let sit = vec![
"amet consectetur adipiscing elit amet",
"consectetur adipiscing elit amet consectetur.",
];
}
fn lorem() {
let ipsum = dolor();
let sit = vec![
"amet consectetur adipiscing elit amet",
"consectetur adipiscing elit amet consectetur.",
];
}
See also: hard_tabs
.
How to handle trailing commas for lists
- Default value:
"Vertical"
- Possible values:
"Always"
,"Never"
,"Vertical"
- Stable: No (tracking issue: #3379)
fn main() {
let Lorem { ipsum, dolor, sit } = amet;
let Lorem {
ipsum,
dolor,
sit,
amet,
consectetur,
adipiscing,
} = elit;
}
fn main() {
let Lorem { ipsum, dolor, sit, } = amet;
let Lorem {
ipsum,
dolor,
sit,
amet,
consectetur,
adipiscing,
} = elit;
}
fn main() {
let Lorem { ipsum, dolor, sit } = amet;
let Lorem {
ipsum,
dolor,
sit,
amet,
consectetur,
adipiscing
} = elit;
}
See also: match_block_trailing_comma
.
Add trailing semicolon after break, continue and return
- Default value:
true
- Possible values:
true
,false
- Stable: No (tracking issue: #3378)
fn foo() -> usize {
return 0;
}
fn foo() -> usize {
return 0
}
Determines if +
or =
are wrapped in spaces in the punctuation of types
- Default value:
"Wide"
- Possible values:
"Compressed"
,"Wide"
- Stable: No (tracking issue: #3364)
fn lorem<Ipsum: Dolor + Sit = Amet>() {
// body
}
fn lorem<Ipsum: Dolor+Sit=Amet>() {
// body
}
Enable unstable features on stable and beta channels (unstable features are available by default on nightly).
- Default value:
false
- Possible values:
true
,false
- Stable: Yes
Note - if you are setting unstable configuration options via the CLI instead of the configuration file, then you should include a unstable_features=true
item before any of the unstable items.
For example:
rustfmt src/lib.rs --config unstable_features=true merge_imports=true
Use field initialize shorthand if possible.
- Default value:
false
- Possible values:
true
,false
- Stable: Yes
struct Foo {
x: u32,
y: u32,
z: u32,
}
fn main() {
let x = 1;
let y = 2;
let z = 3;
let a = Foo { x: x, y: y, z: z };
}
struct Foo {
x: u32,
y: u32,
z: u32,
}
fn main() {
let x = 1;
let y = 2;
let z = 3;
let a = Foo { x, y, z };
}
This option can be used to simplify the management and bulk updates of the granular width configuration settings (fn_call_width
, attr_fn_like_width
, struct_lit_width
, struct_variant_width
, array_width
, chain_width
, single_line_if_else_max_width
), that respectively control when formatted constructs are multi-lined/vertical based on width.
Note that explicitly provided values for the width configuration settings take precedence and override the calculated values determined by width_heuristics
.
- Default value:
"Scaled"
- Possible values:
"Scaled"
,"Off"
,"Max"
- Stable: Yes
When width_heuristics
is set to Scaled
, the values for the granular width settings are calculated as a ratio of the value for max_width
.
The ratios are:
fn_call_width
-60%
attr_fn_like_width
-70%
struct_lit_width
-18%
struct_variant_width
-35%
array_width
-60%
chain_width
-60%
single_line_if_else_max_width
-50%
For example when max_width
is set to 100
, the width settings are:
fn_call_width=60
attr_fn_like_width=70
struct_lit_width=18
struct_variant_width=35
array_width=60
chain_width=60
single_line_if_else_max_width=50
and when max_width
is set to 200
:
fn_call_width=120
attr_fn_like_width=140
struct_lit_width=36
struct_variant_width=70
array_width=120
chain_width=120
single_line_if_else_max_width=100
enum Lorem {
Ipsum,
Dolor(bool),
Sit { amet: Consectetur, adipiscing: Elit },
}
fn main() {
lorem(
"lorem",
"ipsum",
"dolor",
"sit",
"amet",
"consectetur",
"adipiscing",
);
let lorem = Lorem {
ipsum: dolor,
sit: amet,
};
let lorem = Lorem { ipsum: dolor };
let lorem = if ipsum { dolor } else { sit };
}
When width_heuristics
is set to Off
, the granular width settings are functionally disabled and ignored. See the documentation for the respective width config options for specifics.
enum Lorem {
Ipsum,
Dolor(bool),
Sit {
amet: Consectetur,
adipiscing: Elit,
},
}
fn main() {
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
let lorem = Lorem {
ipsum: dolor,
sit: amet,
};
let lorem = if ipsum {
dolor
} else {
sit
};
}
When width_heuristics
is set to Max
, then each granular width setting is set to the same value as max_width
.
So if max_width
is set to 200
, then all the width settings are also set to 200
.
fn_call_width=200
attr_fn_like_width=200
struct_lit_width=200
struct_variant_width=200
array_width=200
chain_width=200
single_line_if_else_max_width=200
enum Lorem {
Ipsum,
Dolor(bool),
Sit { amet: Consectetur, adipiscing: Elit },
}
fn main() {
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
let lorem = Lorem { ipsum: dolor, sit: amet };
let lorem = if ipsum { dolor } else { sit };
}
See also:
max_width
fn_call_width
attr_fn_like_width
struct_lit_width
struct_variant_width
array_width
chain_width
single_line_if_else_max_width
Replace uses of the try! macro by the ? shorthand
- Default value:
false
- Possible values:
true
,false
- Stable: Yes
fn main() {
let lorem = r#try!(ipsum.map(|dolor| dolor.sit()));
}
fn main() {
let lorem = ipsum.map(|dolor| dolor.sit())?;
}
Forces the where
clause to be laid out on a single line.
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3359)
impl<T> Lorem for T
where
Option<T>: Ipsum,
{
// body
}
impl<T> Lorem for T
where Option<T>: Ipsum
{
// body
}
See also brace_style
, control_brace_style
.
Break comments to fit on the line
- Default value:
false
- Possible values:
true
,false
- Stable: No (tracking issue: #3347)
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
// Lorem ipsum dolor sit amet, consectetur adipiscing elit,
// sed do eiusmod tempor incididunt ut labore et dolore
// magna aliqua. Ut enim ad minim veniam, quis nostrud
// exercitation ullamco laboris nisi ut aliquip ex ea
// commodo consequat.