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):
162 #### `"Block"` (default):
170 Adipiscing: Eq = usize,
171 Consectetur: Eq = usize,
178 adipiscing: Adipiscing,
179 consectetur: Consectetur,
189 fn lorem<Ipsum: Eq = usize,
193 Adipiscing: Eq = usize,
194 Consectetur: Eq = usize,
200 adipiscing: Adipiscing,
201 consectetur: Consectetur,
210 #### `"Block"` (default):
225 let lorem = Lorem { ipsum: dolor,
230 See also: [`struct_lit_single_line`](#struct_lit_single_line), [`indent_style`](#indent_style).
234 #### `"Block"` (default):
237 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
251 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
261 ## `use_small_heuristics`
263 Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
265 - **Default value**: `true`
266 - **Possible values**: `true`, `false`
269 #### `true` (default):
275 Sit { amet: Consectetur, adipiscing: Elit },
289 let lorem = Lorem { ipsum: dolor, sit: amet };
291 let lorem = if ipsum { dolor } else { sit };
308 lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
315 let lorem = if ipsum {
325 Where to put a binary operator when a binary expression goes multiline.
327 - **Default value**: `"Front"`
328 - **Possible values**: `"Front"`, `"Back"`
331 #### `"Front"` (default):
342 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
343 ..bbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
350 let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo ||
351 barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
353 let sum = 123456789012345678901234567890 + 123456789012345678901234567890 +
354 123456789012345678901234567890;
356 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..
357 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
361 ## `combine_control_expr`
363 Combine control expressions with function calls.
365 - **Default value**: `true`
366 - **Possible values**: `true`, `false`
369 #### `true` (default):
381 foo!(if let Some(..) = x {
394 foo!(while let Some(..) = x {
428 if let Some(..) = x {
445 while let Some(..) = x {
471 Maximum length of comments. No effect unless`wrap_comments = true`.
473 - **Default value**: `80`
474 - **Possible values**: any positive integer
477 **Note:** A value of `0` results in [`wrap_comments`](#wrap_comments) being applied regardless of a line's width.
479 #### `80` (default; comments shorter than `comment_width`):
481 // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
484 #### `60` (comments longer than `comment_width`):
486 // Lorem ipsum dolor sit amet,
487 // consectetur adipiscing elit.
490 See also [`wrap_comments`](#wrap_comments).
492 ## `condense_wildcard_suffixes`
494 Replace strings of _ wildcards by a single .. in tuple patterns
496 - **Default value**: `false`
497 - **Possible values**: `true`, `false`
500 #### `false` (default):
503 let (lorem, ipsum, _, _) = (1, 2, 3, 4);
509 let (lorem, ipsum, ..) = (1, 2, 3, 4);
512 ## `control_brace_style`
514 Brace style for control flow constructs
516 - **Default value**: `"AlwaysSameLine"`
517 - **Possible values**: `"AlwaysNextLine"`, `"AlwaysSameLine"`, `"ClosingNextLine"`
520 #### `"AlwaysSameLine"` (default):
530 #### `"AlwaysNextLine"`:
543 #### `"ClosingNextLine"`:
554 ## `disable_all_formatting`
556 Don't reformat anything
558 - **Default value**: `false`
559 - **Possible values**: `true`, `false`
562 ## `error_on_line_overflow`
564 Error if unable to get all lines within `max_width`
566 - **Default value**: `true`
567 - **Possible values**: `true`, `false`
570 See also [`max_width`](#max_width).
572 ## `error_on_line_overflow_comments`
574 Error if unable to get all comment lines within `comment_width`.
576 - **Default value**: `true`
577 - **Possible values**: `true`, `false`
580 See also [`comment_width`](#comment_width).
584 Argument density in functions
586 - **Default value**: `"Tall"`
587 - **Possible values**: `"Compressed"`, `"Tall"`, `"Vertical"`
590 #### `"Tall"` (default):
594 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
596 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
605 consectetur: Consectetur,
606 adipiscing: Adipiscing,
615 consectetur: Consectetur,
616 adipiscing: Adipiscing,
628 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
630 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
635 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
636 adipiscing: Adipiscing, elit: Elit,
640 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
641 adipiscing: Adipiscing, elit: Elit,
652 fn lorem(ipsum: Ipsum,
657 fn lorem(ipsum: Ipsum,
664 fn lorem(ipsum: Ipsum,
668 consectetur: Consectetur,
669 adipiscing: Adipiscing,
672 fn lorem(ipsum: Ipsum,
676 consectetur: Consectetur,
677 adipiscing: Adipiscing,
687 Brace style for items
689 - **Default value**: `"SameLineWhere"`
690 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
695 #### `"SameLineWhere"` (default):
702 fn lorem(ipsum: usize) {
706 fn lorem<T>(ipsum: T)
708 T: Add + Sub + Mul + Div,
714 #### `"AlwaysNextLine"`:
722 fn lorem(ipsum: usize)
727 fn lorem<T>(ipsum: T)
729 T: Add + Sub + Mul + Div,
735 #### `"PreferSameLine"`:
742 fn lorem(ipsum: usize) {
746 fn lorem<T>(ipsum: T)
748 T: Add + Sub + Mul + Div, {
753 ### Structs and enums
755 #### `"SameLineWhere"` (default):
769 #### `"AlwaysNextLine"`:
784 #### `"PreferSameLine"`:
798 ## `empty_item_single_line`
800 Put empty-body functions and impls on a single line
802 - **Default value**: `true`
803 - **Possible values**: `true`, `false`
806 #### `true` (default):
824 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
829 Put single-expression functions on a single line
831 - **Default value**: `false`
832 - **Possible values**: `true`, `false`
835 #### `false` (default):
838 fn lorem() -> usize {
842 fn lorem() -> usize {
851 fn lorem() -> usize { 42 }
853 fn lorem() -> usize {
859 See also [`control_brace_style`](#control_brace_style).
862 ## `where_single_line`
864 To force single line where layout
866 - **Default value**: `false`
867 - **Possible values**: `true`, `false`
870 #### `false` (default):
885 where Option<T>: Ipsum {
890 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
893 ## `force_explicit_abi`
895 Always print the abi for extern items
897 - **Default value**: `true`
898 - **Possible values**: `true`, `false`
901 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
903 #### `true` (default):
907 pub static lorem: c_int;
915 pub static lorem: c_int;
921 Format string literals where necessary
923 - **Default value**: `false`
924 - **Possible values**: `true`, `false`
927 #### `false` (default):
930 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
937 "ipsum dolor sit amet consectetur \
938 adipiscing elit lorem ipsum dolor sit";
941 See also [`max_width`](#max_width).
945 Use tab characters for indentation, spaces for alignment
947 - **Default value**: `false`
948 - **Possible values**: `true`, `false`
951 #### `false` (default):
954 fn lorem() -> usize {
955 42 // spaces before 42
962 fn lorem() -> usize {
967 See also: [`tab_spaces`](#tab_spaces).
972 Indent style of imports
974 - **Default Value**: `"Visual"`
975 - **Possible values**: `"Block"`, `"Visual"`
978 #### `"Visual"` (default):
996 See also: [`imports_layout`](#imports_layout).
1000 Item layout inside a imports block
1002 - **Default value**: "Mixed"
1003 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1006 #### `"Mixed"` (default):
1009 use foo::{xxx, yyy, zzz};
1011 use foo::{aaa, bbb, ccc,
1015 #### `"Horizontal"`:
1017 **Note**: This option forces all imports onto one line and may exceed `max_width`.
1020 use foo::{xxx, yyy, zzz};
1022 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1025 #### `"HorizontalVertical"`:
1028 use foo::{xxx, yyy, zzz};
1054 ## `match_block_trailing_comma`
1056 Put a trailing comma after a block based match arm (non-block arms are not affected)
1058 - **Default value**: `false`
1059 - **Possible values**: `true`, `false`
1062 #### `false` (default):
1069 Lorem::Dolor => println!("dolor"),
1080 Lorem::Dolor => println!("dolor"),
1084 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1088 Maximum width of each line
1090 - **Default value**: `100`
1091 - **Possible values**: any positive integer
1094 See also [`error_on_line_overflow`](#error_on_line_overflow).
1098 Merge multiple derives into a single one.
1100 - **Default value**: `true`
1101 - **Possible values**: `true`, `false`
1104 #### `true` (default):
1107 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1114 #[derive(Eq, PartialEq)]
1116 #[derive(Copy, Clone)]
1120 ## `force_multiline_blocks`
1122 Force multiline closure and match arm bodies to be wrapped in a block
1124 - **Default value**: `false`
1125 - **Possible values**: `false`, `true`
1128 #### `false` (default):
1131 result.and_then(|maybe_value| match maybe_value {
1138 println!("Hello World");
1148 result.and_then(|maybe_value| {
1158 println!("Hello World");
1168 Unix or Windows line endings
1170 - **Default value**: `"Unix"`
1171 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1174 ## `normalize_comments`
1176 Convert /* */ comments to // comments where possible
1178 - **Default value**: `false`
1179 - **Possible values**: `true`, `false`
1182 #### `false` (default):
1186 fn dolor() -> usize {}
1189 fn adipiscing() -> usize {}
1196 fn dolor() -> usize {}
1199 fn adipiscing() -> usize {}
1202 ## `reorder_imported_names`
1204 Reorder lists of names in import statements alphabetically
1206 - **Default value**: `false`
1207 - **Possible values**: `true`, `false`
1210 #### `false` (default):
1213 use super::{lorem, ipsum, dolor, sit};
1219 use super::{dolor, ipsum, lorem, sit};
1222 See also [`reorder_imports`](#reorder_imports).
1224 ## `reorder_imports`
1226 Reorder import statements alphabetically
1228 - **Default value**: `false`
1229 - **Possible values**: `true`, `false`
1232 #### `false` (default):
1250 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1252 ## `reorder_imports_in_group`
1254 Reorder import statements in group
1256 - **Default value**: `false`
1257 - **Possible values**: `true`, `false`
1260 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1262 #### `false` (default):
1286 See also [`reorder_imports`](#reorder_imports).
1288 ## `reorder_extern_crates`
1290 Reorder `extern crate` statements alphabetically
1292 - **Default value**: `true`
1293 - **Possible values**: `true`, `false`
1296 #### `true` (default):
1314 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1316 ## `reorder_extern_crates_in_group`
1318 Reorder `extern crate` statements in group
1320 - **Default value**: `true`
1321 - **Possible values**: `true`, `false`
1324 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1326 #### `true` (default):
1350 See also [`reorder_extern_crates`](#reorder_extern_crates).
1354 Report `TODO` items in comments.
1356 - **Default value**: `"Never"`
1357 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1360 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1361 it contains a `#X` (with `X` being a number) in parentheses following the
1362 `TODO`, `"Unnumbered"` will ignore it.
1364 See also [`report_fixme`](#report_fixme).
1368 Report `FIXME` items in comments.
1370 - **Default value**: `"Never"`
1371 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1374 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1375 it contains a `#X` (with `X` being a number) in parentheses following the
1376 `FIXME`, `"Unnumbered"` will ignore it.
1378 See also [`report_todo`](#report_todo).
1383 Don't reformat out of line modules
1385 - **Default value**: `false`
1386 - **Possible values**: `true`, `false`
1389 ## `space_after_colon`
1391 Leave a space after the colon.
1393 - **Default value**: `true`
1394 - **Possible values**: `true`, `false`
1397 #### `true` (default):
1400 fn lorem<T: Eq>(t: T) {
1401 let lorem: Dolor = Lorem {
1411 fn lorem<T:Eq>(t:T) {
1412 let lorem:Dolor = Lorem {
1419 See also: [`space_before_colon`](#space_before_colon).
1421 ## `space_before_colon`
1423 Leave a space before the colon.
1425 - **Default value**: `false`
1426 - **Possible values**: `true`, `false`
1429 #### `false` (default):
1432 fn lorem<T: Eq>(t: T) {
1433 let lorem: Dolor = Lorem {
1443 fn lorem<T : Eq>(t : T) {
1444 let lorem : Dolor = Lorem {
1451 See also: [`space_after_colon`](#space_after_colon).
1453 ## `struct_field_align_threshold`
1455 The maximum diff of width between struct fields to be aligned with each other.
1457 - **Default value** : 0
1458 - **Possible values**: any positive integer
1481 ## `spaces_around_ranges`
1483 Put spaces around the .. and ... range operators
1485 - **Default value**: `false`
1486 - **Possible values**: `true`, `false`
1489 #### `false` (default):
1498 let lorem = 0 .. 10;
1501 ## `spaces_within_parens_and_brackets`
1503 Put spaces within non-empty generic arguments, parentheses, and square brackets
1505 - **Default value**: `false`
1506 - **Possible values**: `true`, `false`
1509 #### `false` (default):
1512 // generic arguments
1513 fn lorem<T: Eq>(t: T) {
1517 // non-empty parentheses
1518 fn lorem<T: Eq>(t: T) {
1519 let lorem = (ipsum, dolor);
1522 // non-empty square brackets
1523 let lorem: [usize; 2] = [ipsum, dolor];
1529 // generic arguments
1530 fn lorem< T: Eq >(t: T) {
1534 // non-empty parentheses
1535 fn lorem<T: Eq>( t: T ) {
1536 let lorem = ( ipsum, dolor );
1539 // non-empty square brackets
1540 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1543 ## `struct_lit_single_line`
1545 Put small struct literals on a single line
1547 - **Default value**: `true`
1548 - **Possible values**: `true`, `false`
1551 #### `true` (default):
1554 let lorem = Lorem { ipsum: dolor, sit: amet };
1566 See also: [`indent_style`](#indent_style).
1571 Number of spaces per tab
1573 - **Default value**: `4`
1574 - **Possible values**: any positive integer
1581 let ipsum = dolor();
1583 "amet consectetur adipiscing elit."
1592 let ipsum = dolor();
1594 "amet consectetur adipiscing elit."
1599 See also: [`hard_tabs`](#hard_tabs).
1604 How to handle trailing commas for lists
1606 - **Default value**: `"Vertical"`
1607 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1610 #### `"Vertical"` (default):
1613 let Lorem { ipsum, dolor, sit } = amet;
1627 let Lorem { ipsum, dolor, sit, } = amet;
1641 let Lorem { ipsum, dolor, sit } = amet;
1652 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1654 ## `trailing_semicolon`
1656 Add trailing semicolon after break, continue and return
1658 - **Default value**: `true`
1659 - **Possible values**: `true`, `false`
1662 #### `true` (default):
1676 ## `type_punctuation_density`
1678 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1680 - **Default value**: `"Wide"`
1681 - **Possible values**: `"Compressed"`, `"Wide"`
1684 #### `"Wide"` (default):
1687 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1692 #### `"Compressed"`:
1695 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1700 ## `use_try_shorthand`
1702 Replace uses of the try! macro by the ? shorthand
1704 - **Default value**: `false`
1705 - **Possible values**: `true`, `false`
1708 #### `false` (default):
1711 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1717 let lorem = ipsum.map(|dolor| dolor.sit())?;
1723 Break comments to fit on the line
1725 - **Default value**: `false`
1726 - **Possible values**: `true`, `false`
1729 #### `false` (default):
1732 // 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.
1738 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1739 // sed do eiusmod tempor incididunt ut labore et dolore
1740 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1741 // exercitation ullamco laboris nisi ut aliquip ex ea
1742 // commodo consequat.
1745 ## `match_arm_blocks`
1747 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1749 - **Default value**: `true`
1750 - **Possible values**: `true`, `false`
1753 #### `true` (default):
1758 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1760 false => println!("{}", sit),
1769 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1770 false => println!("{}", sit),
1774 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1778 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1780 - **Default value**: `"Overwrite"`
1781 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`
1784 ## `blank_lines_upper_bound`
1786 Maximum number of blank lines which can be put between items. If more than this number of consecutive empty
1787 lines are found, they are trimmed down to match this integer.
1789 - **Default value**: `1`
1790 - **Possible values**: *unsigned integer*
1839 See also: [`blank_lines_lower_bound`](#blank_lines_lower_bound)
1841 ## `blank_lines_lower_bound`
1843 Minimum number of blank lines which must be put between items. If two items have fewer blank lines between
1844 them, additional blank lines are inserted.
1846 - **Default value**: `0`
1847 - **Possible values**: *unsigned integer*
1851 Original Code (rustfmt will not change it with the default value of `0`):