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):
48 let lorem = vec!["ipsum",
59 #### `"Block"` (default):
80 See also: [`control_brace_style`](#control_brace_style).
82 ### Function arguments
84 #### `"Block"` (default):
89 fn lorem(ipsum: usize) {}
109 fn lorem(ipsum: usize) {}
111 fn lorem(ipsum: usize,
124 #### `"Block"` (default):
154 #### `"Block"` (default):
162 Adipiscing: Eq = usize,
163 Consectetur: Eq = usize,
170 adipiscing: Adipiscing,
171 consectetur: Consectetur,
181 fn lorem<Ipsum: Eq = usize,
185 Adipiscing: Eq = usize,
186 Consectetur: Eq = usize,
192 adipiscing: Adipiscing,
193 consectetur: Consectetur,
202 #### `"Block"` (default):
214 let lorem = Lorem { ipsum: dolor,
218 See also: [`struct_lit_single_line`](#struct_lit_single_line), [`indent_style`](#indent_style).
222 #### `"Block"` (default):
225 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
239 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
249 ## `use_small_heuristics`
251 Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
253 - **Default value**: `true`
254 - **Possible values**: `true`, `false`
257 #### `true` (default):
263 Sit { amet: Consectetur, adipiscing: Elit },
277 let lorem = Lorem { ipsum: dolor, sit: amet };
279 let lorem = if ipsum { dolor } else { sit };
296 lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
303 let lorem = if ipsum {
313 Where to put a binary operator when a binary expression goes multiline.
315 - **Default value**: `"Front"`
316 - **Possible values**: `"Front"`, `"Back"`
319 #### `"Front"` (default):
330 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
331 ..bbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
338 let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo ||
339 barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
341 let sum = 123456789012345678901234567890 + 123456789012345678901234567890 +
342 123456789012345678901234567890;
344 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
345 ..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // 🐜 See #2364.
349 ## `combine_control_expr`
351 Combine control expressions with function calls.
353 - **Default value**: `true`
354 - **Possible values**: `true`, `false`
357 #### `true` (default):
369 foo!(if let Some(..) = x {
382 foo!(while let Some(..) = x {
416 if let Some(..) = x {
433 while let Some(..) = x {
459 Maximum length of comments. No effect unless`wrap_comments = true`.
461 - **Default value**: `80`
462 - **Possible values**: any positive integer
465 **Note:** A value of `0` results in [`wrap_comments`](#wrap_comments) being applied regardless of a line's width.
467 #### `80` (default; comments shorter than `comment_width`):
469 // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
472 #### `60` (comments longer than `comment_width`):
474 // Lorem ipsum dolor sit amet,
475 // consectetur adipiscing elit.
478 See also [`wrap_comments`](#wrap_comments).
480 ## `condense_wildcard_suffixes`
482 Replace strings of _ wildcards by a single .. in tuple patterns
484 - **Default value**: `false`
485 - **Possible values**: `true`, `false`
488 #### `false` (default):
491 let (lorem, ipsum, _, _) = (1, 2, 3, 4);
497 let (lorem, ipsum, ..) = (1, 2, 3, 4);
500 ## `control_brace_style`
502 Brace style for control flow constructs
504 - **Default value**: `"AlwaysSameLine"`
505 - **Possible values**: `"AlwaysNextLine"`, `"AlwaysSameLine"`, `"ClosingNextLine"`
508 #### `"AlwaysSameLine"` (default):
518 #### `"AlwaysNextLine"`:
531 #### `"ClosingNextLine"`:
542 ## `disable_all_formatting`
544 Don't reformat anything
546 - **Default value**: `false`
547 - **Possible values**: `true`, `false`
550 ## `error_on_line_overflow`
552 Error if unable to get all lines within `max_width`
554 - **Default value**: `true`
555 - **Possible values**: `true`, `false`
558 See also [`max_width`](#max_width).
560 ## `error_on_line_overflow_comments`
562 Error if unable to get all comment lines within `comment_width`.
564 - **Default value**: `true`
565 - **Possible values**: `true`, `false`
568 See also [`comment_width`](#comment_width).
572 Argument density in functions
574 - **Default value**: `"Tall"`
575 - **Possible values**: `"Compressed"`, `"Tall"`, `"Vertical"`
578 #### `"Tall"` (default):
582 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
584 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
593 consectetur: Consectetur,
594 adipiscing: Adipiscing,
603 consectetur: Consectetur,
604 adipiscing: Adipiscing,
616 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
618 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
623 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
624 adipiscing: Adipiscing, elit: Elit,
628 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
629 adipiscing: Adipiscing, elit: Elit,
640 fn lorem(ipsum: Ipsum,
645 fn lorem(ipsum: Ipsum,
652 fn lorem(ipsum: Ipsum,
656 consectetur: Consectetur,
657 adipiscing: Adipiscing,
660 fn lorem(ipsum: Ipsum,
664 consectetur: Consectetur,
665 adipiscing: Adipiscing,
675 Brace style for items
677 - **Default value**: `"SameLineWhere"`
678 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
683 #### `"SameLineWhere"` (default):
690 fn lorem(ipsum: usize) {
694 fn lorem<T>(ipsum: T)
696 T: Add + Sub + Mul + Div,
702 #### `"AlwaysNextLine"`:
710 fn lorem(ipsum: usize)
715 fn lorem<T>(ipsum: T)
717 T: Add + Sub + Mul + Div,
723 #### `"PreferSameLine"`:
730 fn lorem(ipsum: usize) {
734 fn lorem<T>(ipsum: T)
736 T: Add + Sub + Mul + Div, {
741 ### Structs and enums
743 #### `"SameLineWhere"` (default):
757 #### `"AlwaysNextLine"`:
772 #### `"PreferSameLine"`:
786 ## `empty_item_single_line`
788 Put empty-body functions and impls on a single line
790 - **Default value**: `true`
791 - **Possible values**: `true`, `false`
794 #### `true` (default):
812 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
817 Put single-expression functions on a single line
819 - **Default value**: `false`
820 - **Possible values**: `true`, `false`
823 #### `false` (default):
826 fn lorem() -> usize {
830 fn lorem() -> usize {
839 fn lorem() -> usize { 42 }
841 fn lorem() -> usize {
847 See also [`control_brace_style`](#control_brace_style).
850 ## `where_single_line`
852 To force single line where layout
854 - **Default value**: `false`
855 - **Possible values**: `true`, `false`
858 #### `false` (default):
873 where Option<T>: Ipsum {
878 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
881 ## `force_explicit_abi`
883 Always print the abi for extern items
885 - **Default value**: `true`
886 - **Possible values**: `true`, `false`
889 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
891 #### `true` (default):
895 pub static lorem: c_int;
903 pub static lorem: c_int;
909 Format string literals where necessary
911 - **Default value**: `false`
912 - **Possible values**: `true`, `false`
915 #### `false` (default):
918 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
925 "ipsum dolor sit amet consectetur \
926 adipiscing elit lorem ipsum dolor sit";
929 See also [`max_width`](#max_width).
933 Use tab characters for indentation, spaces for alignment
935 - **Default value**: `false`
936 - **Possible values**: `true`, `false`
939 #### `false` (default):
942 fn lorem() -> usize {
943 42 // spaces before 42
950 fn lorem() -> usize {
955 See also: [`tab_spaces`](#tab_spaces).
960 Indent style of imports
962 - **Default Value**: `"Visual"`
963 - **Possible values**: `"Block"`, `"Visual"`
966 #### `"Visual"` (default):
984 See also: [`imports_layout`](#imports_layout).
988 Item layout inside a imports block
990 - **Default value**: "Mixed"
991 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
994 #### `"Mixed"` (default):
997 use foo::{xxx, yyy, zzz};
999 use foo::{aaa, bbb, ccc,
1003 #### `"Horizontal"`:
1005 **Note**: This option forces all imports onto one line and may exceed `max_width`.
1008 use foo::{xxx, yyy, zzz};
1010 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1013 #### `"HorizontalVertical"`:
1016 use foo::{xxx, yyy, zzz};
1042 ## `match_block_trailing_comma`
1044 Put a trailing comma after a block based match arm (non-block arms are not affected)
1046 - **Default value**: `false`
1047 - **Possible values**: `true`, `false`
1050 #### `false` (default):
1057 Lorem::Dolor => println!("dolor"),
1068 Lorem::Dolor => println!("dolor"),
1072 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1076 Maximum width of each line
1078 - **Default value**: `100`
1079 - **Possible values**: any positive integer
1082 See also [`error_on_line_overflow`](#error_on_line_overflow).
1086 Merge multiple derives into a single one.
1088 - **Default value**: `true`
1089 - **Possible values**: `true`, `false`
1092 #### `true` (default):
1095 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1102 #[derive(Eq, PartialEq)]
1104 #[derive(Copy, Clone)]
1108 ## `force_multiline_blocks`
1110 Force multiline closure and match arm bodies to be wrapped in a block
1112 - **Default value**: `false`
1113 - **Possible values**: `false`, `true`
1116 #### `false` (default):
1119 result.and_then(|maybe_value| match maybe_value {
1126 println!("Hello World");
1136 result.and_then(|maybe_value| {
1146 println!("Hello World");
1156 Unix or Windows line endings
1158 - **Default value**: `"Unix"`
1159 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1162 ## `normalize_comments`
1164 Convert /* */ comments to // comments where possible
1166 - **Default value**: `false`
1167 - **Possible values**: `true`, `false`
1170 #### `false` (default):
1174 fn dolor() -> usize {}
1177 fn adipiscing() -> usize {}
1184 fn dolor() -> usize {}
1187 fn adipiscing() -> usize {}
1190 ## `reorder_imported_names`
1192 Reorder lists of names in import statements alphabetically
1194 - **Default value**: `false`
1195 - **Possible values**: `true`, `false`
1198 #### `false` (default):
1201 use super::{lorem, ipsum, dolor, sit};
1207 use super::{dolor, ipsum, lorem, sit};
1210 See also [`reorder_imports`](#reorder_imports).
1212 ## `reorder_imports`
1214 Reorder import statements alphabetically
1216 - **Default value**: `false`
1217 - **Possible values**: `true`, `false`
1220 #### `false` (default):
1238 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1240 ## `reorder_imports_in_group`
1242 Reorder import statements in group
1244 - **Default value**: `false`
1245 - **Possible values**: `true`, `false`
1248 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1250 #### `false` (default):
1274 See also [`reorder_imports`](#reorder_imports).
1276 ## `reorder_extern_crates`
1278 Reorder `extern crate` statements alphabetically
1280 - **Default value**: `true`
1281 - **Possible values**: `true`, `false`
1284 #### `true` (default):
1302 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1304 ## `reorder_extern_crates_in_group`
1306 Reorder `extern crate` statements in group
1308 - **Default value**: `true`
1309 - **Possible values**: `true`, `false`
1312 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1314 #### `true` (default):
1338 See also [`reorder_extern_crates`](#reorder_extern_crates).
1342 Report `TODO` items in comments.
1344 - **Default value**: `"Never"`
1345 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1348 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1349 it contains a `#X` (with `X` being a number) in parentheses following the
1350 `TODO`, `"Unnumbered"` will ignore it.
1352 See also [`report_fixme`](#report_fixme).
1356 Report `FIXME` items in comments.
1358 - **Default value**: `"Never"`
1359 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1362 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1363 it contains a `#X` (with `X` being a number) in parentheses following the
1364 `FIXME`, `"Unnumbered"` will ignore it.
1366 See also [`report_todo`](#report_todo).
1371 Don't reformat out of line modules
1373 - **Default value**: `false`
1374 - **Possible values**: `true`, `false`
1377 ## `space_after_colon`
1379 Leave a space after the colon.
1381 - **Default value**: `true`
1382 - **Possible values**: `true`, `false`
1385 #### `true` (default):
1388 fn lorem<T: Eq>(t: T) {
1389 let lorem: Dolor = Lorem {
1399 fn lorem<T:Eq>(t:T) {
1400 let lorem:Dolor = Lorem {
1407 See also: [`space_before_colon`](#space_before_colon).
1409 ## `space_before_colon`
1411 Leave a space before the colon.
1413 - **Default value**: `false`
1414 - **Possible values**: `true`, `false`
1417 #### `false` (default):
1420 fn lorem<T: Eq>(t: T) {
1421 let lorem: Dolor = Lorem {
1431 fn lorem<T : Eq>(t : T) {
1432 let lorem : Dolor = Lorem {
1439 See also: [`space_after_colon`](#space_after_colon).
1441 ## `struct_field_align_threshold`
1443 The maximum diff of width between struct fields to be aligned with each other.
1445 - **Default value** : 0
1446 - **Possible values**: any positive integer
1469 ## `spaces_around_ranges`
1471 Put spaces around the .. and ... range operators
1473 - **Default value**: `false`
1474 - **Possible values**: `true`, `false`
1477 #### `false` (default):
1486 let lorem = 0 .. 10;
1489 ## `spaces_within_parens_and_brackets`
1491 Put spaces within non-empty generic arguments, parentheses, and square brackets
1493 - **Default value**: `false`
1494 - **Possible values**: `true`, `false`
1497 #### `false` (default):
1500 // generic arguments
1501 fn lorem<T: Eq>(t: T) {
1505 // non-empty parentheses
1506 fn lorem<T: Eq>(t: T) {
1507 let lorem = (ipsum, dolor);
1510 // non-empty square brackets
1511 let lorem: [usize; 2] = [ipsum, dolor];
1517 // generic arguments
1518 fn lorem< T: Eq >(t: T) {
1522 // non-empty parentheses
1523 fn lorem<T: Eq>( t: T ) {
1524 let lorem = ( ipsum, dolor );
1527 // non-empty square brackets
1528 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1531 ## `struct_lit_single_line`
1533 Put small struct literals on a single line
1535 - **Default value**: `true`
1536 - **Possible values**: `true`, `false`
1539 #### `true` (default):
1542 let lorem = Lorem { ipsum: dolor, sit: amet };
1554 See also: [`indent_style`](#indent_style).
1559 Number of spaces per tab
1561 - **Default value**: `4`
1562 - **Possible values**: any positive integer
1569 let ipsum = dolor();
1571 "amet consectetur adipiscing elit."
1580 let ipsum = dolor();
1582 "amet consectetur adipiscing elit."
1587 See also: [`hard_tabs`](#hard_tabs).
1592 How to handle trailing commas for lists
1594 - **Default value**: `"Vertical"`
1595 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1598 #### `"Vertical"` (default):
1601 let Lorem { ipsum, dolor, sit } = amet;
1615 let Lorem { ipsum, dolor, sit, } = amet;
1629 let Lorem { ipsum, dolor, sit } = amet;
1640 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1642 ## `trailing_semicolon`
1644 Add trailing semicolon after break, continue and return
1646 - **Default value**: `true`
1647 - **Possible values**: `true`, `false`
1650 #### `true` (default):
1664 ## `type_punctuation_density`
1666 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1668 - **Default value**: `"Wide"`
1669 - **Possible values**: `"Compressed"`, `"Wide"`
1672 #### `"Wide"` (default):
1675 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1680 #### `"Compressed"`:
1683 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1688 ## `use_try_shorthand`
1690 Replace uses of the try! macro by the ? shorthand
1692 - **Default value**: `false`
1693 - **Possible values**: `true`, `false`
1696 #### `false` (default):
1699 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1705 let lorem = ipsum.map(|dolor| dolor.sit())?;
1711 Break comments to fit on the line
1713 - **Default value**: `false`
1714 - **Possible values**: `true`, `false`
1717 #### `false` (default):
1720 // 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.
1726 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1727 // sed do eiusmod tempor incididunt ut labore et dolore
1728 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1729 // exercitation ullamco laboris nisi ut aliquip ex ea
1730 // commodo consequat.
1733 ## `match_arm_blocks`
1735 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1737 - **Default value**: `true`
1738 - **Possible values**: `true`, `false`
1741 #### `true` (default):
1746 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1748 false => println!("{}", sit),
1757 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1758 false => println!("{}", sit),
1762 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1766 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1768 - **Default value**: `"Overwrite"`
1769 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`
1772 ## `blank_lines_upper_bound`
1774 Maximum number of blank lines which can be put between items. If more than this number of consecutive empty
1775 lines are found, they are trimmed down to match this integer.
1777 - **Default value**: `1`
1778 - **Possible values**: *unsigned integer*
1827 See also: [`blank_lines_lower_bound`](#blank_lines_lower_bound)
1829 ## `blank_lines_lower_bound`
1831 Minimum number of blank lines which must be put between items. If two items have fewer blank lines between
1832 them, additional blank lines are inserted.
1834 - **Default value**: `0`
1835 - **Possible values**: *unsigned integer*
1839 Original Code (rustfmt will not change it with the default value of `0`):