3 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.
5 A possible content of `rustfmt.toml` or `.rustfmt.toml` might look like this:
9 reorder_imports = false
12 Each configuration option is either stable or unstable.
13 Stable options can be used directly, while unstable options are opt-in.
14 To enable unstable options, set `unstable_features = true` in `rustfmt.toml` or pass `--unstable-features` to rustfmt.
16 # Configuration Options
18 Below you find a detailed visual guide on all the supported configuration options of rustfmt:
23 Indent on expressions or items.
25 - **Default value**: `"Block"`
26 - **Possible values**: `"Block"`, `"Visual"`
31 #### `"Block"` (default):
51 let lorem = vec!["ipsum",
63 #### `"Block"` (default):
97 See also: [`control_brace_style`](#control_brace_style).
99 ### Function arguments
101 #### `"Block"` (default):
106 fn lorem(ipsum: usize) {}
126 fn lorem(ipsum: usize) {}
128 fn lorem(ipsum: usize,
141 #### `"Block"` (default):
175 #### `"Block"` (default):
183 Adipiscing: Eq = usize,
184 Consectetur: Eq = usize,
191 adipiscing: Adipiscing,
192 consectetur: Consectetur,
202 fn lorem<Ipsum: Eq = usize,
206 Adipiscing: Eq = usize,
207 Consectetur: Eq = usize,
213 adipiscing: Adipiscing,
214 consectetur: Consectetur,
223 #### `"Block"` (default):
238 let lorem = Lorem { ipsum: dolor,
243 See also: [`struct_lit_single_line`](#struct_lit_single_line), [`indent_style`](#indent_style).
247 #### `"Block"` (default):
250 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
264 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
274 ## `use_small_heuristics`
276 Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
278 - **Default value**: `Default`
279 - **Possible values**: `Default`, `Off`
282 #### `Default` (default):
288 Sit { amet: Consectetur, adipiscing: Elit },
306 let lorem = Lorem { ipsum: dolor };
308 let lorem = if ipsum { dolor } else { sit };
325 lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
332 let lorem = if ipsum {
342 Where to put a binary operator when a binary expression goes multiline.
344 - **Default value**: `"Front"`
345 - **Possible values**: `"Front"`, `"Back"`
348 #### `"Front"` (default):
352 let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo
353 || barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
355 let sum = 123456789012345678901234567890
356 + 123456789012345678901234567890
357 + 123456789012345678901234567890;
359 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
360 ..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
368 let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo ||
369 barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
371 let sum = 123456789012345678901234567890 +
372 123456789012345678901234567890 +
373 123456789012345678901234567890;
375 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..
376 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
380 ## `combine_control_expr`
382 Combine control expressions with function calls.
384 - **Default value**: `true`
385 - **Possible values**: `true`, `false`
388 #### `true` (default):
400 foo!(if let Some(..) = x {
413 foo!(while let Some(..) = x {
447 if let Some(..) = x {
464 while let Some(..) = x {
490 Maximum length of comments. No effect unless`wrap_comments = true`.
492 - **Default value**: `80`
493 - **Possible values**: any positive integer
496 **Note:** A value of `0` results in [`wrap_comments`](#wrap_comments) being applied regardless of a line's width.
498 #### `80` (default; comments shorter than `comment_width`):
500 // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
503 #### `60` (comments longer than `comment_width`):
505 // Lorem ipsum dolor sit amet,
506 // consectetur adipiscing elit.
509 See also [`wrap_comments`](#wrap_comments).
511 ## `condense_wildcard_suffixes`
513 Replace strings of _ wildcards by a single .. in tuple patterns
515 - **Default value**: `false`
516 - **Possible values**: `true`, `false`
519 #### `false` (default):
523 let (lorem, ipsum, _, _) = (1, 2, 3, 4);
524 let (lorem, ipsum, ..) = (1, 2, 3, 4);
532 let (lorem, ipsum, ..) = (1, 2, 3, 4);
536 ## `control_brace_style`
538 Brace style for control flow constructs
540 - **Default value**: `"AlwaysSameLine"`
541 - **Possible values**: `"AlwaysNextLine"`, `"AlwaysSameLine"`, `"ClosingNextLine"`
544 #### `"AlwaysSameLine"` (default):
556 #### `"AlwaysNextLine"`:
571 #### `"ClosingNextLine"`:
584 ## `disable_all_formatting`
586 Don't reformat anything
588 - **Default value**: `false`
589 - **Possible values**: `true`, `false`
592 ## `error_on_line_overflow`
594 Error if Rustfmt is unable to get all lines within `max_width`, except for comments and string
595 literals. If this happens, then it is a bug in Rustfmt. You might be able to work around the bug by
596 refactoring your code to avoid long/complex expressions, usually by extracting a local variable or
597 using a shorter name.
599 - **Default value**: `false`
600 - **Possible values**: `true`, `false`
603 See also [`max_width`](#max_width).
605 ## `error_on_unformatted`
607 Error if unable to get comments or string literals within `max_width`, or they are left with
608 trailing whitespaces.
610 - **Default value**: `false`
611 - **Possible values**: `true`, `false`
616 Argument density in functions
618 - **Default value**: `"Tall"`
619 - **Possible values**: `"Compressed"`, `"Tall"`, `"Vertical"`
622 #### `"Tall"` (default):
626 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
628 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
637 consectetur: Consectetur,
638 adipiscing: Adipiscing,
647 consectetur: Consectetur,
648 adipiscing: Adipiscing,
660 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
662 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
667 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
668 adipiscing: Adipiscing, elit: Elit,
672 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
673 adipiscing: Adipiscing, elit: Elit,
705 consectetur: Consectetur,
706 adipiscing: Adipiscing,
715 consectetur: Consectetur,
716 adipiscing: Adipiscing,
727 Brace style for items
729 - **Default value**: `"SameLineWhere"`
730 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
735 #### `"SameLineWhere"` (default):
742 fn lorem(ipsum: usize) {
746 fn lorem<T>(ipsum: T)
748 T: Add + Sub + Mul + Div,
754 #### `"AlwaysNextLine"`:
762 fn lorem(ipsum: usize)
767 fn lorem<T>(ipsum: T)
769 T: Add + Sub + Mul + Div,
775 #### `"PreferSameLine"`:
782 fn lorem(ipsum: usize) {
786 fn lorem<T>(ipsum: T)
788 T: Add + Sub + Mul + Div, {
793 ### Structs and enums
795 #### `"SameLineWhere"` (default):
810 #### `"AlwaysNextLine"`:
826 #### `"PreferSameLine"`:
841 ## `empty_item_single_line`
843 Put empty-body functions and impls on a single line
845 - **Default value**: `true`
846 - **Possible values**: `true`, `false`
849 #### `true` (default):
867 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
872 Put single-expression functions on a single line
874 - **Default value**: `false`
875 - **Possible values**: `true`, `false`
878 #### `false` (default):
881 fn lorem() -> usize {
885 fn lorem() -> usize {
894 fn lorem() -> usize { 42 }
896 fn lorem() -> usize {
902 See also [`control_brace_style`](#control_brace_style).
905 ## `where_single_line`
907 Forces the `where` clause to be laid out on a single line.
909 - **Default value**: `false`
910 - **Possible values**: `true`, `false`
913 #### `false` (default):
928 where Option<T>: Ipsum
934 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
937 ## `force_explicit_abi`
939 Always print the abi for extern items
941 - **Default value**: `true`
942 - **Possible values**: `true`, `false`
945 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
947 #### `true` (default):
951 pub static lorem: c_int;
959 pub static lorem: c_int;
965 Format string literals where necessary
967 - **Default value**: `false`
968 - **Possible values**: `true`, `false`
971 #### `false` (default):
976 "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit amet consectetur adipiscing";
984 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit amet \
985 consectetur adipiscing";
989 See also [`max_width`](#max_width).
993 Use tab characters for indentation, spaces for alignment
995 - **Default value**: `false`
996 - **Possible values**: `true`, `false`
999 #### `false` (default):
1002 fn lorem() -> usize {
1003 42 // spaces before 42
1010 fn lorem() -> usize {
1011 42 // tabs before 42
1015 See also: [`tab_spaces`](#tab_spaces).
1020 Indent style of imports
1022 - **Default Value**: `"Visual"`
1023 - **Possible values**: `"Block"`, `"Visual"`
1026 #### `"Visual"` (default):
1029 use foo::{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
1030 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz};
1037 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
1038 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
1042 See also: [`imports_layout`](#imports_layout).
1046 Item layout inside a imports block
1048 - **Default value**: "Mixed"
1049 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1052 #### `"Mixed"` (default):
1055 use foo::{xxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz};
1058 aaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbb, cccccccccccccccccc, dddddddddddddddddd,
1059 eeeeeeeeeeeeeeeeee, ffffffffffffffffff,
1063 #### `"Horizontal"`:
1065 **Note**: This option forces all imports onto one line and may exceed `max_width`.
1068 use foo::{xxx, yyy, zzz};
1070 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1073 #### `"HorizontalVertical"`:
1076 use foo::{xxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz};
1109 Merge multiple imports into a single nested import.
1111 - **Default value**: `false`
1112 - **Possible values**: `true`, `false`
1115 #### `false` (default):
1126 use foo::{a, b, c, d, e, f, g};
1130 ## `match_block_trailing_comma`
1132 Put a trailing comma after a block based match arm (non-block arms are not affected)
1134 - **Default value**: `false`
1135 - **Possible values**: `true`, `false`
1138 #### `false` (default):
1146 Lorem::Dolor => println!("dolor"),
1159 Lorem::Dolor => println!("dolor"),
1164 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1168 Maximum width of each line
1170 - **Default value**: `100`
1171 - **Possible values**: any positive integer
1174 See also [`error_on_line_overflow`](#error_on_line_overflow).
1178 Merge multiple derives into a single one.
1180 - **Default value**: `true`
1181 - **Possible values**: `true`, `false`
1184 #### `true` (default):
1187 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1194 #[derive(Eq, PartialEq)]
1196 #[derive(Copy, Clone)]
1200 ## `force_multiline_blocks`
1202 Force multiline closure and match arm bodies to be wrapped in a block
1204 - **Default value**: `false`
1205 - **Possible values**: `false`, `true`
1208 #### `false` (default):
1212 result.and_then(|maybe_value| match maybe_value {
1214 Some(value) => bar(),
1219 println!("Hello World");
1221 Some(dolor) => foo(),
1230 result.and_then(|maybe_value| {
1233 Some(value) => bar(),
1240 println!("Hello World");
1243 Some(dolor) => foo(),
1251 Unix or Windows line endings
1253 - **Default value**: `"Unix"`
1254 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1257 ## `normalize_comments`
1259 Convert /* */ comments to // comments where possible
1261 - **Default value**: `false`
1262 - **Possible values**: `true`, `false`
1265 #### `false` (default):
1269 fn dolor() -> usize {}
1272 fn adipiscing() -> usize {}
1279 fn dolor() -> usize {}
1282 fn adipiscing() -> usize {}
1285 ## `remove_nested_parens`
1287 Remove nested parens.
1289 - **Default value**: `true`,
1290 - **Possible values**: `true`, `false`
1294 #### `true` (default):
1309 ## `reorder_imports`
1311 Reorder import and extern crate statements alphabetically in groups (a group is
1312 separated by a newline).
1314 - **Default value**: `true`
1315 - **Possible values**: `true`, `false`
1318 #### `true` (default):
1337 ## `reorder_modules`
1339 Reorder `mod` declarations alphabetically in group.
1341 - **Default value**: `true`
1342 - **Possible values**: `true`, `false`
1345 #### `true` (default)
1369 **Note** `mod` with `#[macro_export]` will not be reordered since that could change the semantic
1370 of the original source code.
1372 ## `reorder_impl_items`
1374 Reorder impl items. `type` and `const` are put first, then macros and methods.
1376 - **Default value**: `false`
1377 - **Possible values**: `true`, `false`
1380 #### `false` (default)
1385 impl Iterator for Dummy {
1386 fn next(&mut self) -> Option<Self::Item> {
1399 impl Iterator for Dummy {
1402 fn next(&mut self) -> Option<Self::Item> {
1410 Report `TODO` items in comments.
1412 - **Default value**: `"Never"`
1413 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1416 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1417 it contains a `#X` (with `X` being a number) in parentheses following the
1418 `TODO`, `"Unnumbered"` will ignore it.
1420 See also [`report_fixme`](#report_fixme).
1424 Report `FIXME` items in comments.
1426 - **Default value**: `"Never"`
1427 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1430 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1431 it contains a `#X` (with `X` being a number) in parentheses following the
1432 `FIXME`, `"Unnumbered"` will ignore it.
1434 See also [`report_todo`](#report_todo).
1439 Don't reformat out of line modules
1441 - **Default value**: `false`
1442 - **Possible values**: `true`, `false`
1445 ## `space_after_colon`
1447 Leave a space after the colon.
1449 - **Default value**: `true`
1450 - **Possible values**: `true`, `false`
1453 #### `true` (default):
1456 fn lorem<T: Eq>(t: T) {
1457 let lorem: Dolor = Lorem {
1467 fn lorem<T:Eq>(t:T) {
1468 let lorem:Dolor = Lorem {
1475 See also: [`space_before_colon`](#space_before_colon).
1477 ## `space_before_colon`
1479 Leave a space before the colon.
1481 - **Default value**: `false`
1482 - **Possible values**: `true`, `false`
1485 #### `false` (default):
1488 fn lorem<T: Eq>(t: T) {
1489 let lorem: Dolor = Lorem {
1499 fn lorem<T : Eq>(t : T) {
1500 let lorem : Dolor = Lorem {
1507 See also: [`space_after_colon`](#space_after_colon).
1509 ## `struct_field_align_threshold`
1511 The maximum diff of width between struct fields to be aligned with each other.
1513 - **Default value** : 0
1514 - **Possible values**: any positive integer
1537 ## `spaces_around_ranges`
1539 Put spaces around the .., ..=, and ... range operators
1541 - **Default value**: `false`
1542 - **Possible values**: `true`, `false`
1545 #### `false` (default):
1573 let lorem = 0 .. 10;
1574 let ipsum = 0 ..= 10;
1593 ## `struct_lit_single_line`
1595 Put small struct literals on a single line
1597 - **Default value**: `true`
1598 - **Possible values**: `true`, `false`
1601 #### `true` (default):
1605 let lorem = Lorem { foo: bar, baz: ofo };
1620 See also: [`indent_style`](#indent_style).
1625 Number of spaces per tab
1627 - **Default value**: `4`
1628 - **Possible values**: any positive integer
1635 let ipsum = dolor();
1637 "amet consectetur adipiscing elit amet",
1638 "consectetur adipiscing elit amet consectetur.",
1647 let ipsum = dolor();
1649 "amet consectetur adipiscing elit amet",
1650 "consectetur adipiscing elit amet consectetur.",
1655 See also: [`hard_tabs`](#hard_tabs).
1660 How to handle trailing commas for lists
1662 - **Default value**: `"Vertical"`
1663 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1666 #### `"Vertical"` (default):
1670 let Lorem { ipsum, dolor, sit } = amet;
1686 let Lorem { ipsum, dolor, sit, } = amet;
1702 let Lorem { ipsum, dolor, sit } = amet;
1714 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1716 ## `trailing_semicolon`
1718 Add trailing semicolon after break, continue and return
1720 - **Default value**: `true`
1721 - **Possible values**: `true`, `false`
1724 #### `true` (default):
1738 ## `type_punctuation_density`
1740 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1742 - **Default value**: `"Wide"`
1743 - **Possible values**: `"Compressed"`, `"Wide"`
1746 #### `"Wide"` (default):
1749 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1754 #### `"Compressed"`:
1757 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1762 ## `use_field_init_shorthand`
1764 Use field initialize shorthand if possible.
1766 - **Default value**: `false`
1767 - **Possible values**: `true`, `false`
1770 #### `false` (default):
1783 let a = Foo { x: x, y: y, z: z };
1800 let a = Foo { x, y, z };
1804 ## `use_try_shorthand`
1806 Replace uses of the try! macro by the ? shorthand
1808 - **Default value**: `false`
1809 - **Possible values**: `true`, `false`
1812 #### `false` (default):
1816 let lorem = try!(ipsum.map(|dolor| dolor.sit()));
1824 let lorem = ipsum.map(|dolor| dolor.sit())?;
1831 Break comments to fit on the line
1833 - **Default value**: `false`
1834 - **Possible values**: `true`, `false`
1837 #### `false` (default):
1840 // 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.
1846 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1847 // sed do eiusmod tempor incididunt ut labore et dolore
1848 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1849 // exercitation ullamco laboris nisi ut aliquip ex ea
1850 // commodo consequat.
1853 ## `match_arm_blocks`
1855 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1857 - **Default value**: `true`
1858 - **Possible values**: `true`, `false`
1861 #### `true` (default):
1867 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1869 false => println!("{}", sit),
1880 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1881 false => println!("{}", sit),
1886 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1889 ## `blank_lines_upper_bound`
1891 Maximum number of blank lines which can be put between items. If more than this number of consecutive empty
1892 lines are found, they are trimmed down to match this integer.
1894 - **Default value**: `1`
1895 - **Possible values**: *unsigned integer*
1946 See also: [`blank_lines_lower_bound`](#blank_lines_lower_bound)
1948 ## `blank_lines_lower_bound`
1950 Minimum number of blank lines which must be put between items. If two items have fewer blank lines between
1951 them, additional blank lines are inserted.
1953 - **Default value**: `0`
1954 - **Possible values**: *unsigned integer*
1958 Original Code (rustfmt will not change it with the default value of `0`):
1988 ## `required_version`
1990 Require a specific version of rustfmt. If you want to make sure that the
1991 specific version of rustfmt is used in your CI, use this option.
1993 - **Default value**: `CARGO_PKG_VERSION`
1994 - **Possible values**: any published version (e.g. `"0.3.8"`)
1997 ## `hide_parse_errors`
1999 Do not show parse errors if the parser failed to parse files.
2001 - **Default value**: `false`
2002 - **Possible values**: `true`, `false`
2007 Whether to use colored output or not.
2009 - **Default value**: `"Auto"`
2010 - **Possible values**: "Auto", "Always", "Never"
2013 ## `unstable_features`
2015 Enable unstable features on stable channel.
2017 - **Default value**: `false`
2018 - **Possible values**: `true`, `false`
2021 ## `license_template_path`
2023 Check whether beginnings of files match a license template.
2025 - **Default value**: `""``
2026 - **Possible values**: path to a license template file
2029 A license template is a plain text file which is matched literally against the
2030 beginning of each source file, except for `{}`-delimited blocks, which are
2031 matched as regular expressions. The following license template therefore
2032 matches strings like `// Copyright 2017 The Rust Project Developers.`, `//
2033 Copyright 2018 The Rust Project Developers.`, etc.:
2036 // Copyright {\d+} The Rust Project Developers.
2039 `\{`, `\}` and `\\` match literal braces / backslashes.
2043 Skip formatting the specified files and directories.
2045 - **Default value**: format every files
2046 - **Possible values**: See an example below
2051 If you want to ignore specific files, put the following to your config file:
2060 If you want to ignore every file under `examples/`, put the following to your config file:
2074 Internal option, use `--backup`