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-options` 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):
84 See also: [`control_brace_style`](#control_brace_style).
86 ### Function arguments
88 #### `"Block"` (default):
93 fn lorem(ipsum: usize) {}
113 fn lorem(ipsum: usize) {}
115 fn lorem(ipsum: usize,
128 #### `"Block"` (default):
160 #### `"Block"` (default):
168 Adipiscing: Eq = usize,
169 Consectetur: Eq = usize,
176 adipiscing: Adipiscing,
177 consectetur: Consectetur,
187 fn lorem<Ipsum: Eq = usize,
191 Adipiscing: Eq = usize,
192 Consectetur: Eq = usize,
198 adipiscing: Adipiscing,
199 consectetur: Consectetur,
208 #### `"Block"` (default):
220 let lorem = Lorem { ipsum: dolor,
224 See also: [`struct_lit_single_line`](#struct_lit_single_line), [`indent_style`](#indent_style).
228 #### `"Block"` (default):
231 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
245 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
255 ## `use_small_heuristics`
257 Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
259 - **Default value**: `true`
260 - **Possible values**: `true`, `false`
263 #### `true` (default):
269 Sit { amet: Consectetur, adipiscing: Elit },
283 let lorem = Lorem { ipsum: dolor, sit: amet };
285 let lorem = if ipsum { dolor } else { sit };
302 lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
309 let lorem = if ipsum {
319 Where to put a binary operator when a binary expression goes multiline.
321 - **Default value**: `"Front"`
322 - **Possible values**: `"Front"`, `"Back"`
325 #### `"Front"` (default):
336 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
337 ..bbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
344 let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo ||
345 barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
347 let sum = 123456789012345678901234567890 + 123456789012345678901234567890 +
348 123456789012345678901234567890;
350 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..
351 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
355 ## `combine_control_expr`
357 Combine control expressions with function calls.
359 - **Default value**: `true`
360 - **Possible values**: `true`, `false`
363 #### `true` (default):
375 foo!(if let Some(..) = x {
388 foo!(while let Some(..) = x {
422 if let Some(..) = x {
439 while let Some(..) = x {
465 Maximum length of comments. No effect unless`wrap_comments = true`.
467 - **Default value**: `80`
468 - **Possible values**: any positive integer
471 **Note:** A value of `0` results in [`wrap_comments`](#wrap_comments) being applied regardless of a line's width.
473 #### `80` (default; comments shorter than `comment_width`):
475 // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
478 #### `60` (comments longer than `comment_width`):
480 // Lorem ipsum dolor sit amet,
481 // consectetur adipiscing elit.
484 See also [`wrap_comments`](#wrap_comments).
486 ## `condense_wildcard_suffixes`
488 Replace strings of _ wildcards by a single .. in tuple patterns
490 - **Default value**: `false`
491 - **Possible values**: `true`, `false`
494 #### `false` (default):
497 let (lorem, ipsum, _, _) = (1, 2, 3, 4);
503 let (lorem, ipsum, ..) = (1, 2, 3, 4);
506 ## `control_brace_style`
508 Brace style for control flow constructs
510 - **Default value**: `"AlwaysSameLine"`
511 - **Possible values**: `"AlwaysNextLine"`, `"AlwaysSameLine"`, `"ClosingNextLine"`
514 #### `"AlwaysSameLine"` (default):
524 #### `"AlwaysNextLine"`:
537 #### `"ClosingNextLine"`:
548 ## `disable_all_formatting`
550 Don't reformat anything
552 - **Default value**: `false`
553 - **Possible values**: `true`, `false`
556 ## `error_on_line_overflow`
558 Error if unable to get all lines within `max_width`
560 - **Default value**: `true`
561 - **Possible values**: `true`, `false`
564 See also [`max_width`](#max_width).
566 ## `error_on_line_overflow_comments`
568 Error if unable to get all comment lines within `comment_width`.
570 - **Default value**: `true`
571 - **Possible values**: `true`, `false`
574 See also [`comment_width`](#comment_width).
578 Argument density in functions
580 - **Default value**: `"Tall"`
581 - **Possible values**: `"Compressed"`, `"Tall"`, `"Vertical"`
584 #### `"Tall"` (default):
588 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
590 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
599 consectetur: Consectetur,
600 adipiscing: Adipiscing,
609 consectetur: Consectetur,
610 adipiscing: Adipiscing,
622 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
624 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
629 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
630 adipiscing: Adipiscing, elit: Elit,
634 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
635 adipiscing: Adipiscing, elit: Elit,
646 fn lorem(ipsum: Ipsum,
651 fn lorem(ipsum: Ipsum,
658 fn lorem(ipsum: Ipsum,
662 consectetur: Consectetur,
663 adipiscing: Adipiscing,
666 fn lorem(ipsum: Ipsum,
670 consectetur: Consectetur,
671 adipiscing: Adipiscing,
681 Brace style for items
683 - **Default value**: `"SameLineWhere"`
684 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
689 #### `"SameLineWhere"` (default):
696 fn lorem(ipsum: usize) {
700 fn lorem<T>(ipsum: T)
702 T: Add + Sub + Mul + Div,
708 #### `"AlwaysNextLine"`:
716 fn lorem(ipsum: usize)
721 fn lorem<T>(ipsum: T)
723 T: Add + Sub + Mul + Div,
729 #### `"PreferSameLine"`:
736 fn lorem(ipsum: usize) {
740 fn lorem<T>(ipsum: T)
742 T: Add + Sub + Mul + Div, {
747 ### Structs and enums
749 #### `"SameLineWhere"` (default):
763 #### `"AlwaysNextLine"`:
778 #### `"PreferSameLine"`:
792 ## `empty_item_single_line`
794 Put empty-body functions and impls on a single line
796 - **Default value**: `true`
797 - **Possible values**: `true`, `false`
800 #### `true` (default):
818 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
823 Put single-expression functions on a single line
825 - **Default value**: `false`
826 - **Possible values**: `true`, `false`
829 #### `false` (default):
832 fn lorem() -> usize {
836 fn lorem() -> usize {
845 fn lorem() -> usize { 42 }
847 fn lorem() -> usize {
853 See also [`control_brace_style`](#control_brace_style).
856 ## `where_single_line`
858 To force single line where layout
860 - **Default value**: `false`
861 - **Possible values**: `true`, `false`
864 #### `false` (default):
879 where Option<T>: Ipsum {
884 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
887 ## `force_explicit_abi`
889 Always print the abi for extern items
891 - **Default value**: `true`
892 - **Possible values**: `true`, `false`
895 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
897 #### `true` (default):
901 pub static lorem: c_int;
909 pub static lorem: c_int;
915 Format string literals where necessary
917 - **Default value**: `false`
918 - **Possible values**: `true`, `false`
921 #### `false` (default):
924 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
931 "ipsum dolor sit amet consectetur \
932 adipiscing elit lorem ipsum dolor sit";
935 See also [`max_width`](#max_width).
939 Use tab characters for indentation, spaces for alignment
941 - **Default value**: `false`
942 - **Possible values**: `true`, `false`
945 #### `false` (default):
948 fn lorem() -> usize {
949 42 // spaces before 42
956 fn lorem() -> usize {
961 See also: [`tab_spaces`](#tab_spaces).
966 Indent style of imports
968 - **Default Value**: `"Visual"`
969 - **Possible values**: `"Block"`, `"Visual"`
972 #### `"Visual"` (default):
990 See also: [`imports_layout`](#imports_layout).
994 Item layout inside a imports block
996 - **Default value**: "Mixed"
997 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1000 #### `"Mixed"` (default):
1003 use foo::{xxx, yyy, zzz};
1005 use foo::{aaa, bbb, ccc,
1009 #### `"Horizontal"`:
1011 **Note**: This option forces all imports onto one line and may exceed `max_width`.
1014 use foo::{xxx, yyy, zzz};
1016 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1019 #### `"HorizontalVertical"`:
1022 use foo::{xxx, yyy, zzz};
1048 ## `match_block_trailing_comma`
1050 Put a trailing comma after a block based match arm (non-block arms are not affected)
1052 - **Default value**: `false`
1053 - **Possible values**: `true`, `false`
1056 #### `false` (default):
1063 Lorem::Dolor => println!("dolor"),
1074 Lorem::Dolor => println!("dolor"),
1078 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1082 Maximum width of each line
1084 - **Default value**: `100`
1085 - **Possible values**: any positive integer
1088 See also [`error_on_line_overflow`](#error_on_line_overflow).
1092 Merge multiple derives into a single one.
1094 - **Default value**: `true`
1095 - **Possible values**: `true`, `false`
1098 #### `true` (default):
1101 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1108 #[derive(Eq, PartialEq)]
1110 #[derive(Copy, Clone)]
1114 ## `force_multiline_blocks`
1116 Force multiline closure and match arm bodies to be wrapped in a block
1118 - **Default value**: `false`
1119 - **Possible values**: `false`, `true`
1122 #### `false` (default):
1125 result.and_then(|maybe_value| match maybe_value {
1132 println!("Hello World");
1142 result.and_then(|maybe_value| {
1152 println!("Hello World");
1162 Unix or Windows line endings
1164 - **Default value**: `"Unix"`
1165 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1168 ## `normalize_comments`
1170 Convert /* */ comments to // comments where possible
1172 - **Default value**: `false`
1173 - **Possible values**: `true`, `false`
1176 #### `false` (default):
1180 fn dolor() -> usize {}
1183 fn adipiscing() -> usize {}
1190 fn dolor() -> usize {}
1193 fn adipiscing() -> usize {}
1196 ## `reorder_imported_names`
1198 Reorder lists of names in import statements alphabetically
1200 - **Default value**: `false`
1201 - **Possible values**: `true`, `false`
1204 #### `false` (default):
1207 use super::{lorem, ipsum, dolor, sit};
1213 use super::{dolor, ipsum, lorem, sit};
1216 See also [`reorder_imports`](#reorder_imports).
1218 ## `reorder_imports`
1220 Reorder import statements alphabetically
1222 - **Default value**: `false`
1223 - **Possible values**: `true`, `false`
1226 #### `false` (default):
1244 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1246 ## `reorder_imports_in_group`
1248 Reorder import statements in group
1250 - **Default value**: `false`
1251 - **Possible values**: `true`, `false`
1254 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1256 #### `false` (default):
1280 See also [`reorder_imports`](#reorder_imports).
1282 ## `reorder_extern_crates`
1284 Reorder `extern crate` statements alphabetically
1286 - **Default value**: `true`
1287 - **Possible values**: `true`, `false`
1290 #### `true` (default):
1308 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1310 ## `reorder_extern_crates_in_group`
1312 Reorder `extern crate` statements in group
1314 - **Default value**: `true`
1315 - **Possible values**: `true`, `false`
1318 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1320 #### `true` (default):
1344 See also [`reorder_extern_crates`](#reorder_extern_crates).
1348 Report `TODO` items in comments.
1350 - **Default value**: `"Never"`
1351 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1354 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1355 it contains a `#X` (with `X` being a number) in parentheses following the
1356 `TODO`, `"Unnumbered"` will ignore it.
1358 See also [`report_fixme`](#report_fixme).
1362 Report `FIXME` items in comments.
1364 - **Default value**: `"Never"`
1365 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1368 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1369 it contains a `#X` (with `X` being a number) in parentheses following the
1370 `FIXME`, `"Unnumbered"` will ignore it.
1372 See also [`report_todo`](#report_todo).
1377 Don't reformat out of line modules
1379 - **Default value**: `false`
1380 - **Possible values**: `true`, `false`
1383 ## `space_after_colon`
1385 Leave a space after the colon.
1387 - **Default value**: `true`
1388 - **Possible values**: `true`, `false`
1391 #### `true` (default):
1394 fn lorem<T: Eq>(t: T) {
1395 let lorem: Dolor = Lorem {
1405 fn lorem<T:Eq>(t:T) {
1406 let lorem:Dolor = Lorem {
1413 See also: [`space_before_colon`](#space_before_colon).
1415 ## `space_before_colon`
1417 Leave a space before the colon.
1419 - **Default value**: `false`
1420 - **Possible values**: `true`, `false`
1423 #### `false` (default):
1426 fn lorem<T: Eq>(t: T) {
1427 let lorem: Dolor = Lorem {
1437 fn lorem<T : Eq>(t : T) {
1438 let lorem : Dolor = Lorem {
1445 See also: [`space_after_colon`](#space_after_colon).
1447 ## `struct_field_align_threshold`
1449 The maximum diff of width between struct fields to be aligned with each other.
1451 - **Default value** : 0
1452 - **Possible values**: any positive integer
1475 ## `spaces_around_ranges`
1477 Put spaces around the .. and ... range operators
1479 - **Default value**: `false`
1480 - **Possible values**: `true`, `false`
1483 #### `false` (default):
1492 let lorem = 0 .. 10;
1495 ## `spaces_within_parens_and_brackets`
1497 Put spaces within non-empty generic arguments, parentheses, and square brackets
1499 - **Default value**: `false`
1500 - **Possible values**: `true`, `false`
1503 #### `false` (default):
1506 // generic arguments
1507 fn lorem<T: Eq>(t: T) {
1511 // non-empty parentheses
1512 fn lorem<T: Eq>(t: T) {
1513 let lorem = (ipsum, dolor);
1516 // non-empty square brackets
1517 let lorem: [usize; 2] = [ipsum, dolor];
1523 // generic arguments
1524 fn lorem< T: Eq >(t: T) {
1528 // non-empty parentheses
1529 fn lorem<T: Eq>( t: T ) {
1530 let lorem = ( ipsum, dolor );
1533 // non-empty square brackets
1534 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1537 ## `struct_lit_single_line`
1539 Put small struct literals on a single line
1541 - **Default value**: `true`
1542 - **Possible values**: `true`, `false`
1545 #### `true` (default):
1548 let lorem = Lorem { ipsum: dolor, sit: amet };
1560 See also: [`indent_style`](#indent_style).
1565 Number of spaces per tab
1567 - **Default value**: `4`
1568 - **Possible values**: any positive integer
1575 let ipsum = dolor();
1577 "amet consectetur adipiscing elit."
1586 let ipsum = dolor();
1588 "amet consectetur adipiscing elit."
1593 See also: [`hard_tabs`](#hard_tabs).
1598 How to handle trailing commas for lists
1600 - **Default value**: `"Vertical"`
1601 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1604 #### `"Vertical"` (default):
1607 let Lorem { ipsum, dolor, sit } = amet;
1621 let Lorem { ipsum, dolor, sit, } = amet;
1635 let Lorem { ipsum, dolor, sit } = amet;
1646 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1648 ## `trailing_semicolon`
1650 Add trailing semicolon after break, continue and return
1652 - **Default value**: `true`
1653 - **Possible values**: `true`, `false`
1656 #### `true` (default):
1670 ## `type_punctuation_density`
1672 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1674 - **Default value**: `"Wide"`
1675 - **Possible values**: `"Compressed"`, `"Wide"`
1678 #### `"Wide"` (default):
1681 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1686 #### `"Compressed"`:
1689 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1694 ## `use_try_shorthand`
1696 Replace uses of the try! macro by the ? shorthand
1698 - **Default value**: `false`
1699 - **Possible values**: `true`, `false`
1702 #### `false` (default):
1705 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1711 let lorem = ipsum.map(|dolor| dolor.sit())?;
1717 Break comments to fit on the line
1719 - **Default value**: `false`
1720 - **Possible values**: `true`, `false`
1723 #### `false` (default):
1726 // 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.
1732 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1733 // sed do eiusmod tempor incididunt ut labore et dolore
1734 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1735 // exercitation ullamco laboris nisi ut aliquip ex ea
1736 // commodo consequat.
1739 ## `match_arm_blocks`
1741 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1743 - **Default value**: `true`
1744 - **Possible values**: `true`, `false`
1747 #### `true` (default):
1752 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1754 false => println!("{}", sit),
1763 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1764 false => println!("{}", sit),
1768 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1772 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1774 - **Default value**: `"Overwrite"`
1775 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`
1778 ## `blank_lines_upper_bound`
1780 Maximum number of blank lines which can be put between items. If more than this number of consecutive empty
1781 lines are found, they are trimmed down to match this integer.
1783 - **Default value**: `1`
1784 - **Possible values**: *unsigned integer*
1833 See also: [`blank_lines_lower_bound`](#blank_lines_lower_bound)
1835 ## `blank_lines_lower_bound`
1837 Minimum number of blank lines which must be put between items. If two items have fewer blank lines between
1838 them, additional blank lines are inserted.
1840 - **Default value**: `0`
1841 - **Possible values**: *unsigned integer*
1845 Original Code (rustfmt will not change it with the default value of `0`):