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):
1354 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1356 ## `reorder_extern_crates_in_group`
1358 Reorder `extern crate` statements in group
1360 - **Default value**: `true`
1361 - **Possible values**: `true`, `false`
1364 #### `true` (default):
1366 **Note:** This only takes effect when [`reorder_extern_crates`](#reorder_extern_crates) is set to `true`.
1380 This value has no influence beyond the effect of the [`reorder_extern_crates`](#reorder_extern_crates) option. Set [`reorder_extern_crates`](#reorder_extern_crates) to `false` if you do not want `extern crate` groups to be collapsed and ordered.
1384 Report `TODO` items in comments.
1386 - **Default value**: `"Never"`
1387 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1390 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1391 it contains a `#X` (with `X` being a number) in parentheses following the
1392 `TODO`, `"Unnumbered"` will ignore it.
1394 See also [`report_fixme`](#report_fixme).
1398 Report `FIXME` items in comments.
1400 - **Default value**: `"Never"`
1401 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1404 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1405 it contains a `#X` (with `X` being a number) in parentheses following the
1406 `FIXME`, `"Unnumbered"` will ignore it.
1408 See also [`report_todo`](#report_todo).
1413 Don't reformat out of line modules
1415 - **Default value**: `false`
1416 - **Possible values**: `true`, `false`
1419 ## `space_after_colon`
1421 Leave a space after the colon.
1423 - **Default value**: `true`
1424 - **Possible values**: `true`, `false`
1427 #### `true` (default):
1430 fn lorem<T: Eq>(t: T) {
1431 let lorem: Dolor = Lorem {
1441 fn lorem<T:Eq>(t:T) {
1442 let lorem:Dolor = Lorem {
1449 See also: [`space_before_colon`](#space_before_colon).
1451 ## `space_before_colon`
1453 Leave a space before the colon.
1455 - **Default value**: `false`
1456 - **Possible values**: `true`, `false`
1459 #### `false` (default):
1462 fn lorem<T: Eq>(t: T) {
1463 let lorem: Dolor = Lorem {
1473 fn lorem<T : Eq>(t : T) {
1474 let lorem : Dolor = Lorem {
1481 See also: [`space_after_colon`](#space_after_colon).
1483 ## `struct_field_align_threshold`
1485 The maximum diff of width between struct fields to be aligned with each other.
1487 - **Default value** : 0
1488 - **Possible values**: any positive integer
1511 ## `spaces_around_ranges`
1513 Put spaces around the .., ..=, and ... range operators
1515 - **Default value**: `false`
1516 - **Possible values**: `true`, `false`
1519 #### `false` (default):
1547 let lorem = 0 .. 10;
1548 let ipsum = 0 ..= 10;
1567 ## `spaces_within_parens_and_brackets`
1569 Put spaces within non-empty generic arguments, parentheses, and square brackets
1571 - **Default value**: `false`
1572 - **Possible values**: `true`, `false`
1575 #### `false` (default):
1578 // generic arguments
1579 fn lorem<T: Eq>(t: T) {
1583 // non-empty parentheses
1584 fn lorem<T: Eq>(t: T) {
1585 let lorem = (ipsum, dolor);
1588 // non-empty square brackets
1589 fn lorem<T: Eq>(t: T) {
1590 let lorem: [usize; 2] = [ipsum, dolor];
1597 // generic arguments
1598 fn lorem< T: Eq >( t: T ) {
1602 // non-empty parentheses
1603 fn lorem< T: Eq >( t: T ) {
1604 let lorem = ( ipsum, dolor );
1607 // non-empty square brackets
1608 fn lorem< T: Eq >( t: T ) {
1609 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1613 ## `struct_lit_single_line`
1615 Put small struct literals on a single line
1617 - **Default value**: `true`
1618 - **Possible values**: `true`, `false`
1621 #### `true` (default):
1625 let lorem = Lorem { foo: bar, baz: ofo };
1640 See also: [`indent_style`](#indent_style).
1645 Number of spaces per tab
1647 - **Default value**: `4`
1648 - **Possible values**: any positive integer
1655 let ipsum = dolor();
1657 "amet consectetur adipiscing elit amet consectetur adipiscing elit amet consectetur.",
1666 let ipsum = dolor();
1668 "amet consectetur adipiscing elit amet consectetur adipiscing elit amet consectetur.",
1673 See also: [`hard_tabs`](#hard_tabs).
1678 How to handle trailing commas for lists
1680 - **Default value**: `"Vertical"`
1681 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1684 #### `"Vertical"` (default):
1688 let Lorem { ipsum, dolor, sit } = amet;
1704 let Lorem { ipsum, dolor, sit, } = amet;
1720 let Lorem { ipsum, dolor, sit } = amet;
1732 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1734 ## `trailing_semicolon`
1736 Add trailing semicolon after break, continue and return
1738 - **Default value**: `true`
1739 - **Possible values**: `true`, `false`
1742 #### `true` (default):
1756 ## `type_punctuation_density`
1758 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1760 - **Default value**: `"Wide"`
1761 - **Possible values**: `"Compressed"`, `"Wide"`
1764 #### `"Wide"` (default):
1767 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1772 #### `"Compressed"`:
1775 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1780 ## `use_field_init_shorthand`
1782 Use field initialize shorthand if possible.
1784 - **Default value**: `false`
1785 - **Possible values**: `true`, `false`
1788 #### `false` (default):
1801 let a = Foo { x: x, y: y, z: z };
1818 let a = Foo { x, y, z };
1822 ## `use_try_shorthand`
1824 Replace uses of the try! macro by the ? shorthand
1826 - **Default value**: `false`
1827 - **Possible values**: `true`, `false`
1830 #### `false` (default):
1834 let lorem = try!(ipsum.map(|dolor| dolor.sit()));
1842 let lorem = ipsum.map(|dolor| dolor.sit())?;
1849 Break comments to fit on the line
1851 - **Default value**: `false`
1852 - **Possible values**: `true`, `false`
1855 #### `false` (default):
1858 // 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.
1864 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1865 // sed do eiusmod tempor incididunt ut labore et dolore
1866 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1867 // exercitation ullamco laboris nisi ut aliquip ex ea
1868 // commodo consequat.
1871 ## `match_arm_blocks`
1873 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1875 - **Default value**: `true`
1876 - **Possible values**: `true`, `false`
1879 #### `true` (default):
1885 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1887 false => println!("{}", sit),
1898 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1899 false => println!("{}", sit),
1904 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1908 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1910 - **Default value**: `"Overwrite"`
1911 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`
1914 ## `blank_lines_upper_bound`
1916 Maximum number of blank lines which can be put between items. If more than this number of consecutive empty
1917 lines are found, they are trimmed down to match this integer.
1919 - **Default value**: `1`
1920 - **Possible values**: *unsigned integer*
1971 See also: [`blank_lines_lower_bound`](#blank_lines_lower_bound)
1973 ## `blank_lines_lower_bound`
1975 Minimum number of blank lines which must be put between items. If two items have fewer blank lines between
1976 them, additional blank lines are inserted.
1978 - **Default value**: `0`
1979 - **Possible values**: *unsigned integer*
1983 Original Code (rustfmt will not change it with the default value of `0`):