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 # Configuration Options
14 Below you find a detailed visual guide on all the supported configuration options of rustfmt:
19 Indent on expressions or items.
21 - **Default value**: `"Block"`
22 - **Possible values**: `"Block"`, `"Visual"`
26 #### `"Block"` (default):
43 let lorem = vec!["ipsum",
54 #### `"Block"` (default):
75 See also: [`control_brace_style`](#control_brace_style).
77 ### Function arguments
79 #### `"Block"` (default):
84 fn lorem(ipsum: usize) {}
104 fn lorem(ipsum: usize) {}
106 fn lorem(ipsum: usize,
119 #### `"Block"` (default):
149 #### `"Block"` (default):
157 Adipiscing: Eq = usize,
158 Consectetur: Eq = usize,
165 adipiscing: Adipiscing,
166 consectetur: Consectetur,
176 fn lorem<Ipsum: Eq = usize,
180 Adipiscing: Eq = usize,
181 Consectetur: Eq = usize,
187 adipiscing: Adipiscing,
188 consectetur: Consectetur,
197 #### `"Block"` (default):
209 let lorem = Lorem { ipsum: dolor,
213 See also: [`struct_lit_single_line`](#struct_lit_single_line), [`indent_style`](#indent_style).
217 #### `"Block"` (default):
220 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
234 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
245 ## `same_line_attributes`
247 Try to put attributes on the same line as fields and variants
249 - **Default value**: `true`
250 - **Possible values**: `true`, `false`
252 #### `true` (default):
256 #[serde(rename = "Ipsum")] ipsum: usize,
257 #[serde(rename = "Dolor")] dolor: usize,
258 #[serde(rename = "Amet")] amet: usize,
262 #[serde(skip_serializing)] Ipsum,
263 #[serde(skip_serializing)] Dolor,
264 #[serde(skip_serializing)] Amet,
272 #[serde(rename = "Ipsum")]
274 #[serde(rename = "Dolor")]
276 #[serde(rename = "Amet")]
281 #[serde(skip_serializing)]
283 #[serde(skip_serializing)]
285 #[serde(skip_serializing)]
290 ## `use_small_heuristics`
292 Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
294 - **Default value**: `true`
295 - **Possible values**: `true`, `false`
297 #### `true` (default):
303 Sit { amet: Consectetur, adipiscing: Elit },
317 let lorem = Lorem { ipsum: dolor, sit: amet };
319 let lorem = if ipsum { dolor } else { sit };
336 lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
343 let lorem = if ipsum {
353 Where to put a binary operator when a binary expression goes multiline.
355 - **Default value**: `"Front"`
356 - **Possible values**: `"Front"`, `"Back"`
358 #### `"Front"` (default):
369 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
370 ..bbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
384 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..
385 bbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
392 - **Default value**: `"Block"`
393 - **Possible values**: `"Block"`, `"Visual"`
395 #### `"Block"` (default):
410 let lorem = ipsum.dolor()
420 ## `combine_control_expr`
422 Combine control expressions with function calls.
424 - **Default value**: `true`
425 - **Possible values**: `true`, `false`
427 #### `true` (default):
439 foo!(if let Some(..) = x {
452 foo!(while let Some(..) = x {
486 if let Some(..) = x {
503 while let Some(..) = x {
529 Maximum length of comments. No effect unless`wrap_comments = true`.
531 - **Default value**: `80`
532 - **Possible values**: any positive integer
534 **Note:** A value of `0` results in [`wrap_comments`](#wrap_comments) being applied regardless of a line's width.
536 #### Comments shorter than `comment_width`:
538 // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
541 #### Comments longer than `comment_width`:
543 // Lorem ipsum dolor sit amet,
544 // consectetur adipiscing elit.
547 See also [`wrap_comments`](#wrap_comments).
549 ## `condense_wildcard_suffixes`
551 Replace strings of _ wildcards by a single .. in tuple patterns
553 - **Default value**: `false`
554 - **Possible values**: `true`, `false`
556 #### `false` (default):
559 let (lorem, ipsum, _, _) = (1, 2, 3, 4);
565 let (lorem, ipsum, ..) = (1, 2, 3, 4);
568 ## `control_brace_style`
570 Brace style for control flow constructs
572 - **Default value**: `"AlwaysSameLine"`
573 - **Possible values**: `"AlwaysNextLine"`, `"AlwaysSameLine"`, `"ClosingNextLine"`
575 #### `"AlwaysSameLine"` (default):
585 #### `"AlwaysNextLine"`:
598 #### `"ClosingNextLine"`:
609 ## `disable_all_formatting`
611 Don't reformat anything
613 - **Default value**: `false`
614 - **Possible values**: `true`, `false`
616 ## `error_on_line_overflow`
618 Error if unable to get all lines within `max_width`
620 - **Default value**: `true`
621 - **Possible values**: `true`, `false`
623 See also [`max_width`](#max_width).
625 ## `error_on_line_overflow_comments`
627 Error if unable to get all comment lines within `comment_width`.
629 - **Default value**: `true`
630 - **Possible values**: `true`, `false`
632 See also [`comment_width`](#comment_width).
636 Argument density in functions
638 - **Default value**: `"Tall"`
639 - **Possible values**: `"Compressed"`, `"CompressedIfEmpty"`, `"Tall"`, `"Vertical"`
641 #### `"Tall"` (default):
645 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
647 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
656 consectetur: Consectetur,
657 adipiscing: Adipiscing,
666 consectetur: Consectetur,
667 adipiscing: Adipiscing,
679 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
681 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
686 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
687 adipiscing: Adipiscing, elit: Elit,
691 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
692 adipiscing: Adipiscing, elit: Elit,
699 #### `"CompressedIfEmpty"`:
703 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
705 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
710 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
711 adipiscing: Adipiscing, elit: Elit,
719 consectetur: Consectetur,
720 adipiscing: Adipiscing,
732 fn lorem(ipsum: Ipsum,
737 fn lorem(ipsum: Ipsum,
744 fn lorem(ipsum: Ipsum,
748 consectetur: Consectetur,
749 adipiscing: Adipiscing,
752 fn lorem(ipsum: Ipsum,
756 consectetur: Consectetur,
757 adipiscing: Adipiscing,
767 Brace style for items
769 - **Default value**: `"SameLineWhere"`
770 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
774 #### `"SameLineWhere"` (default):
781 fn lorem(ipsum: usize) {
785 fn lorem<T>(ipsum: T)
787 T: Add + Sub + Mul + Div,
793 #### `"AlwaysNextLine"`:
801 fn lorem(ipsum: usize)
806 fn lorem<T>(ipsum: T)
808 T: Add + Sub + Mul + Div,
814 #### `"PreferSameLine"`:
821 fn lorem(ipsum: usize) {
825 fn lorem<T>(ipsum: T)
827 T: Add + Sub + Mul + Div, {
832 ### Structs and enums
834 #### `"SameLineWhere"` (default):
848 #### `"AlwaysNextLine"`:
863 #### `"PreferSameLine"`:
878 ## `fn_empty_single_line`
880 Put empty-body functions on a single line
882 - **Default value**: `true`
883 - **Possible values**: `true`, `false`
885 #### `true` (default):
898 See also [`control_brace_style`](#control_brace_style).
903 Put single-expression functions on a single line
905 - **Default value**: `false`
906 - **Possible values**: `true`, `false`
908 #### `false` (default):
911 fn lorem() -> usize {
915 fn lorem() -> usize {
924 fn lorem() -> usize { 42 }
926 fn lorem() -> usize {
932 See also [`control_brace_style`](#control_brace_style).
934 ## `force_explicit_abi`
936 Always print the abi for extern items
938 - **Default value**: `true`
939 - **Possible values**: `true`, `false`
941 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
943 #### `true` (default):
947 pub static lorem: c_int;
955 pub static lorem: c_int;
961 Format string literals where necessary
963 - **Default value**: `false`
964 - **Possible values**: `true`, `false`
966 #### `false` (default):
969 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
976 "ipsum dolor sit amet consectetur \
977 adipiscing elit lorem ipsum dolor sit";
980 See also [`max_width`](#max_width).
984 Use tab characters for indentation, spaces for alignment
986 - **Default value**: `false`
987 - **Possible values**: `true`, `false`
989 #### `false` (default):
992 fn lorem() -> usize {
993 42 // spaces before 42
1000 fn lorem() -> usize {
1001 42 // tabs before 42
1005 See also: [`tab_spaces`](#tab_spaces).
1007 ## `impl_empty_single_line`
1009 Put empty-body implementations on a single line
1011 - **Default value**: `true`
1012 - **Possible values**: `true`, `false`
1014 #### `true` (default):
1027 See also [`brace_style`](#brace_style).
1029 ## `indent_match_arms`
1031 Indent match arms instead of keeping them at the same indentation level as the match keyword
1033 - **Default value**: `true`
1034 - **Possible values**: `true`, `false`
1036 #### `true` (default):
1058 See also: [`match_block_trailing_comma`](#match_block_trailing_comma), [`wrap_match_arms`](#wrap_match_arms).
1062 Indent style of imports
1064 - **Default Value**: `"Visual"`
1065 - **Possible values**: `"Block"`, `"Visual"`
1067 #### `"Visual"` (default):
1085 See also: [`imports_layout`](#imports_layout).
1089 Item layout inside a imports block
1091 - **Default value**: "Mixed"
1092 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1094 #### `"Mixed"` (default):
1097 use foo::{xxx, yyy, zzz};
1099 use foo::{aaa, bbb, ccc,
1103 #### `"Horizontal"`:
1105 **Note**: This option forces to put everything on one line and may exceeds `max_width`.
1108 use foo::{xxx, yyy, zzz};
1110 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1113 #### `"HorizontalVertical"`:
1116 use foo::{xxx, yyy, zzz};
1141 ## `match_arm_forces_newline`
1143 Consistently put match arms (block based or not) in a newline.
1145 - **Default value**: `false`
1146 - **Possible values**: `true`, `false`
1148 #### `false` (default):
1152 // a non-empty block
1159 X2 => println!("ok"),
1167 // a non-empty block
1181 See also: [`wrap_match_arms`](#wrap_match_arms).
1183 ## `match_block_trailing_comma`
1185 Put a trailing comma after a block based match arm (non-block arms are not affected)
1187 - **Default value**: `false`
1188 - **Possible values**: `true`, `false`
1190 #### `false` (default):
1197 Lorem::Dolor => println!("dolor"),
1208 Lorem::Dolor => println!("dolor"),
1212 See also: [`indent_match_arms`](#indent_match_arms), [`trailing_comma`](#trailing_comma), [`wrap_match_arms`](#wrap_match_arms).
1216 Maximum width of each line
1218 - **Default value**: `100`
1219 - **Possible values**: any positive integer
1221 See also [`error_on_line_overflow`](#error_on_line_overflow).
1225 Merge multiple derives into a single one.
1227 - **Default value**: `true`
1228 - **Possible values**: `true`, `false`
1230 #### `true` (default):
1233 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1240 #[derive(Eq, PartialEq)]
1242 #[derive(Copy, Clone)]
1246 ## `multiline_closure_forces_block`
1248 Force multiline closure bodies to be wrapped in a block
1250 - **Default value**: `false`
1251 - **Possible values**: `false`, `true`
1253 #### `false` (default):
1256 result.and_then(|maybe_value| match maybe_value {
1266 result.and_then(|maybe_value| {
1274 ## `multiline_match_arm_forces_block`
1276 Force multiline match arm bodies to be wrapped in a block
1278 - **Default value**: `false`
1279 - **Possible values**: `false`, `true`
1281 #### `false` (default):
1286 println!("Hello World");
1298 println!("Hello World");
1307 Unix or Windows line endings
1309 - **Default value**: `"Unix"`
1310 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1312 ## `normalize_comments`
1314 Convert /* */ comments to // comments where possible
1316 - **Default value**: `false`
1317 - **Possible values**: `true`, `false`
1319 #### `false` (default):
1323 fn dolor() -> usize {}
1326 fn adipiscing() -> usize {}
1333 fn dolor() -> usize {}
1336 fn adipiscing() -> usize {}
1339 ## `reorder_imported_names`
1341 Reorder lists of names in import statements alphabetically
1343 - **Default value**: `false`
1344 - **Possible values**: `true`, `false`
1346 #### `false` (default):
1349 use super::{lorem, ipsum, dolor, sit};
1355 use super::{dolor, ipsum, lorem, sit};
1358 See also [`reorder_imports`](#reorder_imports).
1360 ## `reorder_imports`
1362 Reorder import statements alphabetically
1364 - **Default value**: `false`
1365 - **Possible values**: `true`, `false`
1367 #### `false` (default):
1385 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1387 ## `reorder_imports_in_group`
1389 Reorder import statements in group
1391 - **Default value**: `false`
1392 - **Possible values**: `true`, `false`
1394 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1396 #### `false` (default):
1420 See also [`reorder_imports`](#reorder_imports).
1422 ## `reorder_extern_crates`
1424 Reorder `extern crate` statements alphabetically
1426 - **Default value**: `true`
1427 - **Possible values**: `true`, `false`
1429 #### `true` (default):
1447 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1449 ## `reorder_extern_crates_in_group`
1451 Reorder `extern crate` statements in group
1453 - **Default value**: `true`
1454 - **Possible values**: `true`, `false`
1456 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1458 #### `true` (default):
1482 See also [`reorder_extern_crates`](#reorder_extern_crates).
1486 Report `TODO` items in comments.
1488 - **Default value**: `"Never"`
1489 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1491 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1492 it contains a `#X` (with `X` being a number) in parentheses following the
1493 `TODO`, `"Unnumbered"` will ignore it.
1495 See also [`report_fixme`](#report_fixme).
1499 Report `FIXME` items in comments.
1501 - **Default value**: `"Never"`
1502 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1504 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1505 it contains a `#X` (with `X` being a number) in parentheses following the
1506 `FIXME`, `"Unnumbered"` will ignore it.
1508 See also [`report_todo`](#report_todo).
1513 Don't reformat out of line modules
1515 - **Default value**: `false`
1516 - **Possible values**: `true`, `false`
1518 ## `space_after_colon`
1520 Leave a space after the colon.
1522 - **Default value**: `true`
1523 - **Possible values**: `true`, `false`
1525 #### `true` (default):
1528 fn lorem<T: Eq>(t: T) {
1529 let lorem: Dolor = Lorem {
1539 fn lorem<T:Eq>(t:T) {
1540 let lorem:Dolor = Lorem {
1547 See also: [`space_before_colon`](#space_before_colon).
1549 ## `space_before_colon`
1551 Leave a space before the colon.
1553 - **Default value**: `false`
1554 - **Possible values**: `true`, `false`
1556 #### `false` (default):
1559 fn lorem<T: Eq>(t: T) {
1560 let lorem: Dolor = Lorem {
1570 fn lorem<T : Eq>(t : T) {
1571 let lorem : Dolor = Lorem {
1578 See also: [`space_after_colon`](#space_after_colon).
1580 ## `struct_field_align_threshold`
1582 The maximum diff of width between struct fields to be aligned with each other.
1584 - **Default value** : 0
1585 - **Possible values**: any positive integer
1609 ## `spaces_around_ranges`
1611 Put spaces around the .. and ... range operators
1613 - **Default value**: `false`
1614 - **Possible values**: `true`, `false`
1616 #### `false` (default):
1625 let lorem = 0 .. 10;
1628 ## `spaces_within_parens_and_brackets`
1630 Put spaces within non-empty generic arguments
1632 - **Default value**: `false`
1633 - **Possible values**: `true`, `false`
1635 #### `false` (default):
1638 fn lorem<T: Eq>(t: T) {
1646 fn lorem< T: Eq >(t: T) {
1651 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1653 ## `spaces_within_parens_and_brackets`
1655 Put spaces within non-empty parentheses
1657 - **Default value**: `false`
1658 - **Possible values**: `true`, `false`
1660 #### `false` (default):
1663 fn lorem<T: Eq>(t: T) {
1664 let lorem = (ipsum, dolor);
1671 fn lorem<T: Eq>( t: T ) {
1672 let lorem = ( ipsum, dolor );
1676 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1678 ## `spaces_within_parens_and_brackets`
1680 Put spaces within non-empty square brackets
1682 - **Default value**: `false`
1683 - **Possible values**: `true`, `false`
1685 #### `false` (default):
1688 let lorem: [usize; 2] = [ipsum, dolor];
1694 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1697 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1699 ## `struct_lit_single_line`
1701 Put small struct literals on a single line
1703 - **Default value**: `true`
1704 - **Possible values**: `true`, `false`
1706 #### `true` (default):
1709 let lorem = Lorem { ipsum: dolor, sit: amet };
1721 See also: [`indent_style`](#indent_style).
1726 Number of spaces per tab
1728 - **Default value**: `4`
1729 - **Possible values**: any positive integer
1735 let ipsum = dolor();
1737 "amet consectetur adipiscing elit."
1746 let ipsum = dolor();
1748 "amet consectetur adipiscing elit."
1753 See also: [`hard_tabs`](#hard_tabs).
1758 How to handle trailing commas for lists
1760 - **Default value**: `"Vertical"`
1761 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1763 #### `"Vertical"` (default):
1766 let Lorem { ipsum, dolor, sit } = amet;
1780 let Lorem { ipsum, dolor, sit, } = amet;
1794 let Lorem { ipsum, dolor, sit } = amet;
1805 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1807 ## `trailing_semicolon`
1809 Add trailing semicolon after break, continue and return
1811 - **Default value**: `true`
1812 - **Possible values**: `true`, `false`
1814 #### `true` (default):
1828 ## `type_punctuation_density`
1830 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1832 - **Default value**: `"Wide"`
1833 - **Possible values**: `"Compressed"`, `"Wide"`
1835 #### `"Wide"` (default):
1838 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1843 #### `"Compressed"`:
1846 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1851 ## `use_try_shorthand`
1853 Replace uses of the try! macro by the ? shorthand
1855 - **Default value**: `false`
1856 - **Possible values**: `true`, `false`
1858 #### `false` (default):
1861 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1867 let lorem = ipsum.map(|dolor| dolor.sit())?;
1873 Break comments to fit on the line
1875 - **Default value**: `false`
1876 - **Possible values**: `true`, `false`
1878 #### `false` (default):
1881 // 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.
1887 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1888 // sed do eiusmod tempor incididunt ut labore et dolore
1889 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1890 // exercitation ullamco laboris nisi ut aliquip ex ea
1891 // commodo consequat.
1894 ## `wrap_match_arms`
1896 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1898 - **Default value**: `true`
1899 - **Possible values**: `true`, `false`
1901 #### `true` (default):
1906 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1908 false => println!("{}", sit),
1917 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1918 false => println!("{}", sit),
1922 See also: [`indent_match_arms`](#indent_match_arms), [`match_block_trailing_comma`](#match_block_trailing_comma).
1926 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1928 - **Default value**: `"Overwrite"`
1929 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`