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_imported_names = true
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):
67 if lorem_ipsum && dolor_sit && amet_consectetur && lorem_sit && dolor_consectetur && amet_ipsum
79 if lorem_ipsum && dolor_sit && amet_consectetur && lorem_sit && dolor_consectetur && amet_ipsum
87 See also: [`control_brace_style`](#control_brace_style).
89 ### Function arguments
91 #### `"Block"` (default):
96 fn lorem(ipsum: usize) {}
116 fn lorem(ipsum: usize) {}
118 fn lorem(ipsum: usize,
131 #### `"Block"` (default):
165 #### `"Block"` (default):
173 Adipiscing: Eq = usize,
174 Consectetur: Eq = usize,
181 adipiscing: Adipiscing,
182 consectetur: Consectetur,
192 fn lorem<Ipsum: Eq = usize,
196 Adipiscing: Eq = usize,
197 Consectetur: Eq = usize,
203 adipiscing: Adipiscing,
204 consectetur: Consectetur,
213 #### `"Block"` (default):
228 let lorem = Lorem { ipsum: dolor,
233 See also: [`struct_lit_single_line`](#struct_lit_single_line), [`indent_style`](#indent_style).
237 #### `"Block"` (default):
240 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
254 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
264 ## `use_small_heuristics`
266 Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
268 - **Default value**: `true`
269 - **Possible values**: `true`, `false`
272 #### `true` (default):
278 Sit { amet: Consectetur, adipiscing: Elit },
296 let lorem = Lorem { ipsum: dolor };
298 let lorem = if ipsum { dolor } else { sit };
315 lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
322 let lorem = if ipsum {
332 Where to put a binary operator when a binary expression goes multiline.
334 - **Default value**: `"Front"`
335 - **Possible values**: `"Front"`, `"Back"`
338 #### `"Front"` (default):
342 let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo
343 || barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
345 let sum = 123456789012345678901234567890 + 123456789012345678901234567890
346 + 123456789012345678901234567890;
348 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
349 ..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
357 let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo ||
358 barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
360 let sum = 123456789012345678901234567890 + 123456789012345678901234567890 +
361 123456789012345678901234567890;
363 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..
364 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
368 ## `combine_control_expr`
370 Combine control expressions with function calls.
372 - **Default value**: `true`
373 - **Possible values**: `true`, `false`
376 #### `true` (default):
388 foo!(if let Some(..) = x {
401 foo!(while let Some(..) = x {
435 if let Some(..) = x {
452 while let Some(..) = x {
478 Maximum length of comments. No effect unless`wrap_comments = true`.
480 - **Default value**: `80`
481 - **Possible values**: any positive integer
484 **Note:** A value of `0` results in [`wrap_comments`](#wrap_comments) being applied regardless of a line's width.
486 #### `80` (default; comments shorter than `comment_width`):
488 // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
491 #### `60` (comments longer than `comment_width`):
493 // Lorem ipsum dolor sit amet,
494 // consectetur adipiscing elit.
497 See also [`wrap_comments`](#wrap_comments).
499 ## `condense_wildcard_suffixes`
501 Replace strings of _ wildcards by a single .. in tuple patterns
503 - **Default value**: `false`
504 - **Possible values**: `true`, `false`
507 #### `false` (default):
511 let (lorem, ipsum, _, _) = (1, 2, 3, 4);
512 let (lorem, ipsum, ..) = (1, 2, 3, 4);
520 let (lorem, ipsum, ..) = (1, 2, 3, 4);
524 ## `control_brace_style`
526 Brace style for control flow constructs
528 - **Default value**: `"AlwaysSameLine"`
529 - **Possible values**: `"AlwaysNextLine"`, `"AlwaysSameLine"`, `"ClosingNextLine"`
532 #### `"AlwaysSameLine"` (default):
544 #### `"AlwaysNextLine"`:
559 #### `"ClosingNextLine"`:
572 ## `disable_all_formatting`
574 Don't reformat anything
576 - **Default value**: `false`
577 - **Possible values**: `true`, `false`
580 ## `error_on_line_overflow`
582 Error if unable to get all lines within `max_width`
584 - **Default value**: `true`
585 - **Possible values**: `true`, `false`
588 See also [`max_width`](#max_width).
590 ## `error_on_line_overflow_comments`
592 Error if unable to get all comment lines within `comment_width`.
594 - **Default value**: `true`
595 - **Possible values**: `true`, `false`
598 See also [`comment_width`](#comment_width).
602 Argument density in functions
604 - **Default value**: `"Tall"`
605 - **Possible values**: `"Compressed"`, `"Tall"`, `"Vertical"`
608 #### `"Tall"` (default):
612 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
614 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
623 consectetur: Consectetur,
624 adipiscing: Adipiscing,
633 consectetur: Consectetur,
634 adipiscing: Adipiscing,
646 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
648 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
653 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
654 adipiscing: Adipiscing, elit: Elit,
658 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
659 adipiscing: Adipiscing, elit: Elit,
691 consectetur: Consectetur,
692 adipiscing: Adipiscing,
701 consectetur: Consectetur,
702 adipiscing: Adipiscing,
713 Brace style for items
715 - **Default value**: `"SameLineWhere"`
716 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
721 #### `"SameLineWhere"` (default):
728 fn lorem(ipsum: usize) {
732 fn lorem<T>(ipsum: T)
734 T: Add + Sub + Mul + Div,
740 #### `"AlwaysNextLine"`:
748 fn lorem(ipsum: usize)
753 fn lorem<T>(ipsum: T)
755 T: Add + Sub + Mul + Div,
761 #### `"PreferSameLine"`:
768 fn lorem(ipsum: usize) {
772 fn lorem<T>(ipsum: T)
774 T: Add + Sub + Mul + Div, {
779 ### Structs and enums
781 #### `"SameLineWhere"` (default):
796 #### `"AlwaysNextLine"`:
812 #### `"PreferSameLine"`:
827 ## `empty_item_single_line`
829 Put empty-body functions and impls on a single line
831 - **Default value**: `true`
832 - **Possible values**: `true`, `false`
835 #### `true` (default):
853 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
858 Put single-expression functions on a single line
860 - **Default value**: `false`
861 - **Possible values**: `true`, `false`
864 #### `false` (default):
867 fn lorem() -> usize {
871 fn lorem() -> usize {
880 fn lorem() -> usize { 42 }
882 fn lorem() -> usize {
888 See also [`control_brace_style`](#control_brace_style).
891 ## `where_single_line`
893 To force single line where layout
895 - **Default value**: `false`
896 - **Possible values**: `true`, `false`
899 #### `false` (default):
914 where Option<T>: Ipsum
920 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
923 ## `force_explicit_abi`
925 Always print the abi for extern items
927 - **Default value**: `true`
928 - **Possible values**: `true`, `false`
931 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
933 #### `true` (default):
937 pub static lorem: c_int;
945 pub static lorem: c_int;
951 Format string literals where necessary
953 - **Default value**: `false`
954 - **Possible values**: `true`, `false`
957 #### `false` (default):
962 "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit amet consectetur adipiscing";
970 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit amet \
971 consectetur adipiscing";
975 See also [`max_width`](#max_width).
979 Use tab characters for indentation, spaces for alignment
981 - **Default value**: `false`
982 - **Possible values**: `true`, `false`
985 #### `false` (default):
988 fn lorem() -> usize {
989 42 // spaces before 42
996 fn lorem() -> usize {
1001 See also: [`tab_spaces`](#tab_spaces).
1006 Indent style of imports
1008 - **Default Value**: `"Visual"`
1009 - **Possible values**: `"Block"`, `"Visual"`
1012 #### `"Visual"` (default):
1015 use foo::{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
1016 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz};
1023 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
1024 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
1028 See also: [`imports_layout`](#imports_layout).
1032 Item layout inside a imports block
1034 - **Default value**: "Mixed"
1035 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1038 #### `"Mixed"` (default):
1041 use foo::{xxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz};
1043 use foo::{aaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbb, cccccccccccccccccc, dddddddddddddddddd,
1044 eeeeeeeeeeeeeeeeee, ffffffffffffffffff};
1047 #### `"Horizontal"`:
1049 **Note**: This option forces all imports onto one line and may exceed `max_width`.
1052 use foo::{xxx, yyy, zzz};
1054 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1057 #### `"HorizontalVertical"`:
1060 use foo::{xxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz};
1062 use foo::{aaaaaaaaaaaaaaaaaa,
1067 ffffffffffffffffff};
1086 ## `match_block_trailing_comma`
1088 Put a trailing comma after a block based match arm (non-block arms are not affected)
1090 - **Default value**: `false`
1091 - **Possible values**: `true`, `false`
1094 #### `false` (default):
1102 Lorem::Dolor => println!("dolor"),
1115 Lorem::Dolor => println!("dolor"),
1120 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1124 Maximum width of each line
1126 - **Default value**: `100`
1127 - **Possible values**: any positive integer
1130 See also [`error_on_line_overflow`](#error_on_line_overflow).
1134 Merge multiple derives into a single one.
1136 - **Default value**: `true`
1137 - **Possible values**: `true`, `false`
1140 #### `true` (default):
1143 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1150 #[derive(Eq, PartialEq)]
1152 #[derive(Copy, Clone)]
1156 ## `force_multiline_blocks`
1158 Force multiline closure and match arm bodies to be wrapped in a block
1160 - **Default value**: `false`
1161 - **Possible values**: `false`, `true`
1164 #### `false` (default):
1168 result.and_then(|maybe_value| match maybe_value {
1170 Some(value) => bar(),
1175 println!("Hello World");
1177 Some(dolor) => foo(),
1186 result.and_then(|maybe_value| {
1189 Some(value) => bar(),
1196 println!("Hello World");
1199 Some(dolor) => foo(),
1207 Unix or Windows line endings
1209 - **Default value**: `"Unix"`
1210 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1213 ## `normalize_comments`
1215 Convert /* */ comments to // comments where possible
1217 - **Default value**: `false`
1218 - **Possible values**: `true`, `false`
1221 #### `false` (default):
1225 fn dolor() -> usize {}
1228 fn adipiscing() -> usize {}
1235 fn dolor() -> usize {}
1238 fn adipiscing() -> usize {}
1241 ## `reorder_imported_names`
1243 Reorder lists of names in import statements alphabetically
1245 - **Default value**: `false`
1246 - **Possible values**: `true`, `false`
1249 #### `false` (default):
1252 use super::{lorem, ipsum, dolor, sit};
1258 use super::{dolor, ipsum, lorem, sit};
1261 See also [`reorder_imports`](#reorder_imports).
1263 ## `reorder_imports`
1265 Reorder import statements alphabetically
1267 - **Default value**: `false`
1268 - **Possible values**: `true`, `false`
1271 #### `false` (default):
1289 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1291 ## `reorder_imports_in_group`
1293 Reorder import statements in group
1295 - **Default value**: `false`
1296 - **Possible values**: `true`, `false`
1299 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1301 #### `false` (default):
1325 See also [`reorder_imports`](#reorder_imports).
1327 ## `reorder_extern_crates`
1329 Reorder `extern crate` statements alphabetically
1331 - **Default value**: `true`
1332 - **Possible values**: `true`, `false`
1335 #### `true` (default):
1353 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1355 ## `reorder_extern_crates_in_group`
1357 Reorder `extern crate` statements in group
1359 - **Default value**: `true`
1360 - **Possible values**: `true`, `false`
1363 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1365 #### `true` (default):
1389 See also [`reorder_extern_crates`](#reorder_extern_crates).
1393 Report `TODO` items in comments.
1395 - **Default value**: `"Never"`
1396 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1399 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1400 it contains a `#X` (with `X` being a number) in parentheses following the
1401 `TODO`, `"Unnumbered"` will ignore it.
1403 See also [`report_fixme`](#report_fixme).
1407 Report `FIXME` items in comments.
1409 - **Default value**: `"Never"`
1410 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1413 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1414 it contains a `#X` (with `X` being a number) in parentheses following the
1415 `FIXME`, `"Unnumbered"` will ignore it.
1417 See also [`report_todo`](#report_todo).
1422 Don't reformat out of line modules
1424 - **Default value**: `false`
1425 - **Possible values**: `true`, `false`
1428 ## `space_after_colon`
1430 Leave a space after the colon.
1432 - **Default value**: `true`
1433 - **Possible values**: `true`, `false`
1436 #### `true` (default):
1439 fn lorem<T: Eq>(t: T) {
1440 let lorem: Dolor = Lorem {
1450 fn lorem<T:Eq>(t:T) {
1451 let lorem:Dolor = Lorem {
1458 See also: [`space_before_colon`](#space_before_colon).
1460 ## `space_before_colon`
1462 Leave a space before the colon.
1464 - **Default value**: `false`
1465 - **Possible values**: `true`, `false`
1468 #### `false` (default):
1471 fn lorem<T: Eq>(t: T) {
1472 let lorem: Dolor = Lorem {
1482 fn lorem<T : Eq>(t : T) {
1483 let lorem : Dolor = Lorem {
1490 See also: [`space_after_colon`](#space_after_colon).
1492 ## `struct_field_align_threshold`
1494 The maximum diff of width between struct fields to be aligned with each other.
1496 - **Default value** : 0
1497 - **Possible values**: any positive integer
1520 ## `spaces_around_ranges`
1522 Put spaces around the .., ..=, and ... range operators
1524 - **Default value**: `false`
1525 - **Possible values**: `true`, `false`
1528 #### `false` (default):
1556 let lorem = 0 .. 10;
1557 let ipsum = 0 ..= 10;
1576 ## `spaces_within_parens_and_brackets`
1578 Put spaces within non-empty generic arguments, parentheses, and square brackets
1580 - **Default value**: `false`
1581 - **Possible values**: `true`, `false`
1584 #### `false` (default):
1587 // generic arguments
1588 fn lorem<T: Eq>(t: T) {
1592 // non-empty parentheses
1593 fn lorem<T: Eq>(t: T) {
1594 let lorem = (ipsum, dolor);
1597 // non-empty square brackets
1598 fn lorem<T: Eq>(t: T) {
1599 let lorem: [usize; 2] = [ipsum, dolor];
1606 // generic arguments
1607 fn lorem< T: Eq >( t: T ) {
1611 // non-empty parentheses
1612 fn lorem< T: Eq >( t: T ) {
1613 let lorem = ( ipsum, dolor );
1616 // non-empty square brackets
1617 fn lorem< T: Eq >( t: T ) {
1618 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1622 ## `struct_lit_single_line`
1624 Put small struct literals on a single line
1626 - **Default value**: `true`
1627 - **Possible values**: `true`, `false`
1630 #### `true` (default):
1633 let lorem = Lorem { ipsum: dolor, sit: amet };
1647 See also: [`indent_style`](#indent_style).
1652 Number of spaces per tab
1654 - **Default value**: `4`
1655 - **Possible values**: any positive integer
1662 let ipsum = dolor();
1664 "amet consectetur adipiscing elit amet consectetur adipiscing elit amet consectetur.",
1673 let ipsum = dolor();
1675 "amet consectetur adipiscing elit amet consectetur adipiscing elit amet consectetur.",
1680 See also: [`hard_tabs`](#hard_tabs).
1685 How to handle trailing commas for lists
1687 - **Default value**: `"Vertical"`
1688 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1691 #### `"Vertical"` (default):
1695 let Lorem { ipsum, dolor, sit } = amet;
1711 let Lorem { ipsum, dolor, sit, } = amet;
1727 let Lorem { ipsum, dolor, sit } = amet;
1739 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1741 ## `trailing_semicolon`
1743 Add trailing semicolon after break, continue and return
1745 - **Default value**: `true`
1746 - **Possible values**: `true`, `false`
1749 #### `true` (default):
1763 ## `type_punctuation_density`
1765 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1767 - **Default value**: `"Wide"`
1768 - **Possible values**: `"Compressed"`, `"Wide"`
1771 #### `"Wide"` (default):
1774 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1779 #### `"Compressed"`:
1782 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1787 ## `use_field_init_shorthand`
1789 Use field initialize shorthand if possible.
1791 - **Default value**: `false`
1792 - **Possible values**: `true`, `false`
1795 #### `false` (default):
1808 let a = Foo { x: x, y: y, z: z };
1825 let a = Foo { x, y, z };
1829 ## `use_try_shorthand`
1831 Replace uses of the try! macro by the ? shorthand
1833 - **Default value**: `false`
1834 - **Possible values**: `true`, `false`
1837 #### `false` (default):
1841 let lorem = try!(ipsum.map(|dolor| dolor.sit()));
1849 let lorem = ipsum.map(|dolor| dolor.sit())?;
1856 Break comments to fit on the line
1858 - **Default value**: `false`
1859 - **Possible values**: `true`, `false`
1862 #### `false` (default):
1865 // 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.
1871 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1872 // sed do eiusmod tempor incididunt ut labore et dolore
1873 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1874 // exercitation ullamco laboris nisi ut aliquip ex ea
1875 // commodo consequat.
1878 ## `match_arm_blocks`
1880 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1882 - **Default value**: `true`
1883 - **Possible values**: `true`, `false`
1886 #### `true` (default):
1892 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1894 false => println!("{}", sit),
1905 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1906 false => println!("{}", sit),
1911 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1915 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1917 - **Default value**: `"Overwrite"`
1918 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`
1921 ## `blank_lines_upper_bound`
1923 Maximum number of blank lines which can be put between items. If more than this number of consecutive empty
1924 lines are found, they are trimmed down to match this integer.
1926 - **Default value**: `1`
1927 - **Possible values**: *unsigned integer*
1978 See also: [`blank_lines_lower_bound`](#blank_lines_lower_bound)
1980 ## `blank_lines_lower_bound`
1982 Minimum number of blank lines which must be put between items. If two items have fewer blank lines between
1983 them, additional blank lines are inserted.
1985 - **Default value**: `0`
1986 - **Possible values**: *unsigned integer*
1990 Original Code (rustfmt will not change it with the default value of `0`):