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"`:
877 ## `empty_item_single_line`
879 Put empty-body functions and impls on a single line
881 - **Default value**: `true`
882 - **Possible values**: `true`, `false`
884 #### `true` (default):
902 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
907 Put single-expression functions on a single line
909 - **Default value**: `false`
910 - **Possible values**: `true`, `false`
912 #### `false` (default):
915 fn lorem() -> usize {
919 fn lorem() -> usize {
928 fn lorem() -> usize { 42 }
930 fn lorem() -> usize {
936 See also [`control_brace_style`](#control_brace_style).
938 ## `force_explicit_abi`
940 Always print the abi for extern items
942 - **Default value**: `true`
943 - **Possible values**: `true`, `false`
945 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
947 #### `true` (default):
951 pub static lorem: c_int;
959 pub static lorem: c_int;
965 Format string literals where necessary
967 - **Default value**: `false`
968 - **Possible values**: `true`, `false`
970 #### `false` (default):
973 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
980 "ipsum dolor sit amet consectetur \
981 adipiscing elit lorem ipsum dolor sit";
984 See also [`max_width`](#max_width).
988 Use tab characters for indentation, spaces for alignment
990 - **Default value**: `false`
991 - **Possible values**: `true`, `false`
993 #### `false` (default):
996 fn lorem() -> usize {
997 42 // spaces before 42
1004 fn lorem() -> usize {
1005 42 // tabs before 42
1009 See also: [`tab_spaces`](#tab_spaces).
1014 Indent style of imports
1016 - **Default Value**: `"Visual"`
1017 - **Possible values**: `"Block"`, `"Visual"`
1019 #### `"Visual"` (default):
1037 See also: [`imports_layout`](#imports_layout).
1041 Item layout inside a imports block
1043 - **Default value**: "Mixed"
1044 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1046 #### `"Mixed"` (default):
1049 use foo::{xxx, yyy, zzz};
1051 use foo::{aaa, bbb, ccc,
1055 #### `"Horizontal"`:
1057 **Note**: This option forces to put everything on one line and may exceeds `max_width`.
1060 use foo::{xxx, yyy, zzz};
1062 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1065 #### `"HorizontalVertical"`:
1068 use foo::{xxx, yyy, zzz};
1094 ## `match_block_trailing_comma`
1096 Put a trailing comma after a block based match arm (non-block arms are not affected)
1098 - **Default value**: `false`
1099 - **Possible values**: `true`, `false`
1101 #### `false` (default):
1108 Lorem::Dolor => println!("dolor"),
1119 Lorem::Dolor => println!("dolor"),
1123 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1127 Maximum width of each line
1129 - **Default value**: `100`
1130 - **Possible values**: any positive integer
1132 See also [`error_on_line_overflow`](#error_on_line_overflow).
1136 Merge multiple derives into a single one.
1138 - **Default value**: `true`
1139 - **Possible values**: `true`, `false`
1141 #### `true` (default):
1144 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1151 #[derive(Eq, PartialEq)]
1153 #[derive(Copy, Clone)]
1157 ## `force_multiline_blocks`
1159 Force multiline closure and match arm bodies to be wrapped in a block
1161 - **Default value**: `false`
1162 - **Possible values**: `false`, `true`
1164 #### `false` (default):
1167 result.and_then(|maybe_value| match maybe_value {
1174 println!("Hello World");
1184 result.and_then(|maybe_value| {
1194 println!("Hello World");
1204 Unix or Windows line endings
1206 - **Default value**: `"Unix"`
1207 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1209 ## `normalize_comments`
1211 Convert /* */ comments to // comments where possible
1213 - **Default value**: `false`
1214 - **Possible values**: `true`, `false`
1216 #### `false` (default):
1220 fn dolor() -> usize {}
1223 fn adipiscing() -> usize {}
1230 fn dolor() -> usize {}
1233 fn adipiscing() -> usize {}
1236 ## `reorder_imported_names`
1238 Reorder lists of names in import statements alphabetically
1240 - **Default value**: `false`
1241 - **Possible values**: `true`, `false`
1243 #### `false` (default):
1246 use super::{lorem, ipsum, dolor, sit};
1252 use super::{dolor, ipsum, lorem, sit};
1255 See also [`reorder_imports`](#reorder_imports).
1257 ## `reorder_imports`
1259 Reorder import statements alphabetically
1261 - **Default value**: `false`
1262 - **Possible values**: `true`, `false`
1264 #### `false` (default):
1282 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1284 ## `reorder_imports_in_group`
1286 Reorder import statements in group
1288 - **Default value**: `false`
1289 - **Possible values**: `true`, `false`
1291 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1293 #### `false` (default):
1317 See also [`reorder_imports`](#reorder_imports).
1319 ## `reorder_extern_crates`
1321 Reorder `extern crate` statements alphabetically
1323 - **Default value**: `true`
1324 - **Possible values**: `true`, `false`
1326 #### `true` (default):
1344 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1346 ## `reorder_extern_crates_in_group`
1348 Reorder `extern crate` statements in group
1350 - **Default value**: `true`
1351 - **Possible values**: `true`, `false`
1353 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1355 #### `true` (default):
1379 See also [`reorder_extern_crates`](#reorder_extern_crates).
1383 Report `TODO` items in comments.
1385 - **Default value**: `"Never"`
1386 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1388 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1389 it contains a `#X` (with `X` being a number) in parentheses following the
1390 `TODO`, `"Unnumbered"` will ignore it.
1392 See also [`report_fixme`](#report_fixme).
1396 Report `FIXME` items in comments.
1398 - **Default value**: `"Never"`
1399 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1401 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1402 it contains a `#X` (with `X` being a number) in parentheses following the
1403 `FIXME`, `"Unnumbered"` will ignore it.
1405 See also [`report_todo`](#report_todo).
1410 Don't reformat out of line modules
1412 - **Default value**: `false`
1413 - **Possible values**: `true`, `false`
1415 ## `space_after_colon`
1417 Leave a space after the colon.
1419 - **Default value**: `true`
1420 - **Possible values**: `true`, `false`
1422 #### `true` (default):
1425 fn lorem<T: Eq>(t: T) {
1426 let lorem: Dolor = Lorem {
1436 fn lorem<T:Eq>(t:T) {
1437 let lorem:Dolor = Lorem {
1444 See also: [`space_before_colon`](#space_before_colon).
1446 ## `space_before_colon`
1448 Leave a space before the colon.
1450 - **Default value**: `false`
1451 - **Possible values**: `true`, `false`
1453 #### `false` (default):
1456 fn lorem<T: Eq>(t: T) {
1457 let lorem: Dolor = Lorem {
1467 fn lorem<T : Eq>(t : T) {
1468 let lorem : Dolor = Lorem {
1475 See also: [`space_after_colon`](#space_after_colon).
1477 ## `struct_field_align_threshold`
1479 The maximum diff of width between struct fields to be aligned with each other.
1481 - **Default value** : 0
1482 - **Possible values**: any positive integer
1506 ## `spaces_around_ranges`
1508 Put spaces around the .. and ... range operators
1510 - **Default value**: `false`
1511 - **Possible values**: `true`, `false`
1513 #### `false` (default):
1522 let lorem = 0 .. 10;
1525 ## `spaces_within_parens_and_brackets`
1527 Put spaces within non-empty generic arguments
1529 - **Default value**: `false`
1530 - **Possible values**: `true`, `false`
1532 #### `false` (default):
1535 fn lorem<T: Eq>(t: T) {
1543 fn lorem< T: Eq >(t: T) {
1548 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1550 ## `spaces_within_parens_and_brackets`
1552 Put spaces within non-empty parentheses
1554 - **Default value**: `false`
1555 - **Possible values**: `true`, `false`
1557 #### `false` (default):
1560 fn lorem<T: Eq>(t: T) {
1561 let lorem = (ipsum, dolor);
1568 fn lorem<T: Eq>( t: T ) {
1569 let lorem = ( ipsum, dolor );
1573 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1575 ## `spaces_within_parens_and_brackets`
1577 Put spaces within non-empty square brackets
1579 - **Default value**: `false`
1580 - **Possible values**: `true`, `false`
1582 #### `false` (default):
1585 let lorem: [usize; 2] = [ipsum, dolor];
1591 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1594 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1596 ## `struct_lit_single_line`
1598 Put small struct literals on a single line
1600 - **Default value**: `true`
1601 - **Possible values**: `true`, `false`
1603 #### `true` (default):
1606 let lorem = Lorem { ipsum: dolor, sit: amet };
1618 See also: [`indent_style`](#indent_style).
1623 Number of spaces per tab
1625 - **Default value**: `4`
1626 - **Possible values**: any positive integer
1632 let ipsum = dolor();
1634 "amet consectetur adipiscing elit."
1643 let ipsum = dolor();
1645 "amet consectetur adipiscing elit."
1650 See also: [`hard_tabs`](#hard_tabs).
1655 How to handle trailing commas for lists
1657 - **Default value**: `"Vertical"`
1658 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1660 #### `"Vertical"` (default):
1663 let Lorem { ipsum, dolor, sit } = amet;
1677 let Lorem { ipsum, dolor, sit, } = amet;
1691 let Lorem { ipsum, dolor, sit } = amet;
1702 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1704 ## `trailing_semicolon`
1706 Add trailing semicolon after break, continue and return
1708 - **Default value**: `true`
1709 - **Possible values**: `true`, `false`
1711 #### `true` (default):
1725 ## `type_punctuation_density`
1727 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1729 - **Default value**: `"Wide"`
1730 - **Possible values**: `"Compressed"`, `"Wide"`
1732 #### `"Wide"` (default):
1735 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1740 #### `"Compressed"`:
1743 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1748 ## `use_try_shorthand`
1750 Replace uses of the try! macro by the ? shorthand
1752 - **Default value**: `false`
1753 - **Possible values**: `true`, `false`
1755 #### `false` (default):
1758 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1764 let lorem = ipsum.map(|dolor| dolor.sit())?;
1770 Break comments to fit on the line
1772 - **Default value**: `false`
1773 - **Possible values**: `true`, `false`
1775 #### `false` (default):
1778 // 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.
1784 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1785 // sed do eiusmod tempor incididunt ut labore et dolore
1786 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1787 // exercitation ullamco laboris nisi ut aliquip ex ea
1788 // commodo consequat.
1791 ## `match_arm_blocks`
1793 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1795 - **Default value**: `true`
1796 - **Possible values**: `true`, `false`
1798 #### `true` (default):
1803 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1805 false => println!("{}", sit),
1814 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1815 false => println!("{}", sit),
1819 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1823 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1825 - **Default value**: `"Overwrite"`
1826 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`