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"`, `"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,
703 fn lorem(ipsum: Ipsum,
708 fn lorem(ipsum: Ipsum,
715 fn lorem(ipsum: Ipsum,
719 consectetur: Consectetur,
720 adipiscing: Adipiscing,
723 fn lorem(ipsum: Ipsum,
727 consectetur: Consectetur,
728 adipiscing: Adipiscing,
738 Brace style for items
740 - **Default value**: `"SameLineWhere"`
741 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
745 #### `"SameLineWhere"` (default):
752 fn lorem(ipsum: usize) {
756 fn lorem<T>(ipsum: T)
758 T: Add + Sub + Mul + Div,
764 #### `"AlwaysNextLine"`:
772 fn lorem(ipsum: usize)
777 fn lorem<T>(ipsum: T)
779 T: Add + Sub + Mul + Div,
785 #### `"PreferSameLine"`:
792 fn lorem(ipsum: usize) {
796 fn lorem<T>(ipsum: T)
798 T: Add + Sub + Mul + Div, {
803 ### Structs and enums
805 #### `"SameLineWhere"` (default):
819 #### `"AlwaysNextLine"`:
834 #### `"PreferSameLine"`:
848 ## `empty_item_single_line`
850 Put empty-body functions and impls on a single line
852 - **Default value**: `true`
853 - **Possible values**: `true`, `false`
855 #### `true` (default):
873 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
878 Put single-expression functions on a single line
880 - **Default value**: `false`
881 - **Possible values**: `true`, `false`
883 #### `false` (default):
886 fn lorem() -> usize {
890 fn lorem() -> usize {
899 fn lorem() -> usize { 42 }
901 fn lorem() -> usize {
907 See also [`control_brace_style`](#control_brace_style).
910 ## `where_single_line`
912 To force single line where layout
914 - **Default value**: `false`
915 - **Possible values**: `true`, `false`
917 #### `false` (default):
932 where Option<T>: Ipsum {
937 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
940 ## `force_explicit_abi`
942 Always print the abi for extern items
944 - **Default value**: `true`
945 - **Possible values**: `true`, `false`
947 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
949 #### `true` (default):
953 pub static lorem: c_int;
961 pub static lorem: c_int;
967 Format string literals where necessary
969 - **Default value**: `false`
970 - **Possible values**: `true`, `false`
972 #### `false` (default):
975 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
982 "ipsum dolor sit amet consectetur \
983 adipiscing elit lorem ipsum dolor sit";
986 See also [`max_width`](#max_width).
990 Use tab characters for indentation, spaces for alignment
992 - **Default value**: `false`
993 - **Possible values**: `true`, `false`
995 #### `false` (default):
998 fn lorem() -> usize {
999 42 // spaces before 42
1006 fn lorem() -> usize {
1007 42 // tabs before 42
1011 See also: [`tab_spaces`](#tab_spaces).
1016 Indent style of imports
1018 - **Default Value**: `"Visual"`
1019 - **Possible values**: `"Block"`, `"Visual"`
1021 #### `"Visual"` (default):
1039 See also: [`imports_layout`](#imports_layout).
1043 Item layout inside a imports block
1045 - **Default value**: "Mixed"
1046 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1048 #### `"Mixed"` (default):
1051 use foo::{xxx, yyy, zzz};
1053 use foo::{aaa, bbb, ccc,
1057 #### `"Horizontal"`:
1059 **Note**: This option forces to put everything on one line and may exceeds `max_width`.
1062 use foo::{xxx, yyy, zzz};
1064 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1067 #### `"HorizontalVertical"`:
1070 use foo::{xxx, yyy, zzz};
1096 ## `match_block_trailing_comma`
1098 Put a trailing comma after a block based match arm (non-block arms are not affected)
1100 - **Default value**: `false`
1101 - **Possible values**: `true`, `false`
1103 #### `false` (default):
1110 Lorem::Dolor => println!("dolor"),
1121 Lorem::Dolor => println!("dolor"),
1125 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1129 Maximum width of each line
1131 - **Default value**: `100`
1132 - **Possible values**: any positive integer
1134 See also [`error_on_line_overflow`](#error_on_line_overflow).
1138 Merge multiple derives into a single one.
1140 - **Default value**: `true`
1141 - **Possible values**: `true`, `false`
1143 #### `true` (default):
1146 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1153 #[derive(Eq, PartialEq)]
1155 #[derive(Copy, Clone)]
1159 ## `force_multiline_blocks`
1161 Force multiline closure and match arm bodies to be wrapped in a block
1163 - **Default value**: `false`
1164 - **Possible values**: `false`, `true`
1166 #### `false` (default):
1169 result.and_then(|maybe_value| match maybe_value {
1176 println!("Hello World");
1186 result.and_then(|maybe_value| {
1196 println!("Hello World");
1206 Unix or Windows line endings
1208 - **Default value**: `"Unix"`
1209 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1211 ## `normalize_comments`
1213 Convert /* */ comments to // comments where possible
1215 - **Default value**: `false`
1216 - **Possible values**: `true`, `false`
1218 #### `false` (default):
1222 fn dolor() -> usize {}
1225 fn adipiscing() -> usize {}
1232 fn dolor() -> usize {}
1235 fn adipiscing() -> usize {}
1238 ## `reorder_imported_names`
1240 Reorder lists of names in import statements alphabetically
1242 - **Default value**: `false`
1243 - **Possible values**: `true`, `false`
1245 #### `false` (default):
1248 use super::{lorem, ipsum, dolor, sit};
1254 use super::{dolor, ipsum, lorem, sit};
1257 See also [`reorder_imports`](#reorder_imports).
1259 ## `reorder_imports`
1261 Reorder import statements alphabetically
1263 - **Default value**: `false`
1264 - **Possible values**: `true`, `false`
1266 #### `false` (default):
1284 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1286 ## `reorder_imports_in_group`
1288 Reorder import statements in group
1290 - **Default value**: `false`
1291 - **Possible values**: `true`, `false`
1293 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1295 #### `false` (default):
1319 See also [`reorder_imports`](#reorder_imports).
1321 ## `reorder_extern_crates`
1323 Reorder `extern crate` statements alphabetically
1325 - **Default value**: `true`
1326 - **Possible values**: `true`, `false`
1328 #### `true` (default):
1346 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1348 ## `reorder_extern_crates_in_group`
1350 Reorder `extern crate` statements in group
1352 - **Default value**: `true`
1353 - **Possible values**: `true`, `false`
1355 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1357 #### `true` (default):
1381 See also [`reorder_extern_crates`](#reorder_extern_crates).
1385 Report `TODO` items in comments.
1387 - **Default value**: `"Never"`
1388 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1390 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1391 it contains a `#X` (with `X` being a number) in parentheses following the
1392 `TODO`, `"Unnumbered"` will ignore it.
1394 See also [`report_fixme`](#report_fixme).
1398 Report `FIXME` items in comments.
1400 - **Default value**: `"Never"`
1401 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1403 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1404 it contains a `#X` (with `X` being a number) in parentheses following the
1405 `FIXME`, `"Unnumbered"` will ignore it.
1407 See also [`report_todo`](#report_todo).
1412 Don't reformat out of line modules
1414 - **Default value**: `false`
1415 - **Possible values**: `true`, `false`
1417 ## `space_after_colon`
1419 Leave a space after the colon.
1421 - **Default value**: `true`
1422 - **Possible values**: `true`, `false`
1424 #### `true` (default):
1427 fn lorem<T: Eq>(t: T) {
1428 let lorem: Dolor = Lorem {
1438 fn lorem<T:Eq>(t:T) {
1439 let lorem:Dolor = Lorem {
1446 See also: [`space_before_colon`](#space_before_colon).
1448 ## `space_before_colon`
1450 Leave a space before the colon.
1452 - **Default value**: `false`
1453 - **Possible values**: `true`, `false`
1455 #### `false` (default):
1458 fn lorem<T: Eq>(t: T) {
1459 let lorem: Dolor = Lorem {
1469 fn lorem<T : Eq>(t : T) {
1470 let lorem : Dolor = Lorem {
1477 See also: [`space_after_colon`](#space_after_colon).
1479 ## `struct_field_align_threshold`
1481 The maximum diff of width between struct fields to be aligned with each other.
1483 - **Default value** : 0
1484 - **Possible values**: any positive integer
1508 ## `spaces_around_ranges`
1510 Put spaces around the .. and ... range operators
1512 - **Default value**: `false`
1513 - **Possible values**: `true`, `false`
1515 #### `false` (default):
1524 let lorem = 0 .. 10;
1527 ## `spaces_within_parens_and_brackets`
1529 Put spaces within non-empty generic arguments
1531 - **Default value**: `false`
1532 - **Possible values**: `true`, `false`
1534 #### `false` (default):
1537 fn lorem<T: Eq>(t: T) {
1545 fn lorem< T: Eq >(t: T) {
1550 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1552 ## `spaces_within_parens_and_brackets`
1554 Put spaces within non-empty parentheses
1556 - **Default value**: `false`
1557 - **Possible values**: `true`, `false`
1559 #### `false` (default):
1562 fn lorem<T: Eq>(t: T) {
1563 let lorem = (ipsum, dolor);
1570 fn lorem<T: Eq>( t: T ) {
1571 let lorem = ( ipsum, dolor );
1575 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1577 ## `spaces_within_parens_and_brackets`
1579 Put spaces within non-empty square brackets
1581 - **Default value**: `false`
1582 - **Possible values**: `true`, `false`
1584 #### `false` (default):
1587 let lorem: [usize; 2] = [ipsum, dolor];
1593 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1596 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1598 ## `struct_lit_single_line`
1600 Put small struct literals on a single line
1602 - **Default value**: `true`
1603 - **Possible values**: `true`, `false`
1605 #### `true` (default):
1608 let lorem = Lorem { ipsum: dolor, sit: amet };
1620 See also: [`indent_style`](#indent_style).
1625 Number of spaces per tab
1627 - **Default value**: `4`
1628 - **Possible values**: any positive integer
1634 let ipsum = dolor();
1636 "amet consectetur adipiscing elit."
1645 let ipsum = dolor();
1647 "amet consectetur adipiscing elit."
1652 See also: [`hard_tabs`](#hard_tabs).
1657 How to handle trailing commas for lists
1659 - **Default value**: `"Vertical"`
1660 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1662 #### `"Vertical"` (default):
1665 let Lorem { ipsum, dolor, sit } = amet;
1679 let Lorem { ipsum, dolor, sit, } = amet;
1693 let Lorem { ipsum, dolor, sit } = amet;
1704 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1706 ## `trailing_semicolon`
1708 Add trailing semicolon after break, continue and return
1710 - **Default value**: `true`
1711 - **Possible values**: `true`, `false`
1713 #### `true` (default):
1727 ## `type_punctuation_density`
1729 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1731 - **Default value**: `"Wide"`
1732 - **Possible values**: `"Compressed"`, `"Wide"`
1734 #### `"Wide"` (default):
1737 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1742 #### `"Compressed"`:
1745 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1750 ## `use_try_shorthand`
1752 Replace uses of the try! macro by the ? shorthand
1754 - **Default value**: `false`
1755 - **Possible values**: `true`, `false`
1757 #### `false` (default):
1760 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1766 let lorem = ipsum.map(|dolor| dolor.sit())?;
1772 Break comments to fit on the line
1774 - **Default value**: `false`
1775 - **Possible values**: `true`, `false`
1777 #### `false` (default):
1780 // 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.
1786 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1787 // sed do eiusmod tempor incididunt ut labore et dolore
1788 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1789 // exercitation ullamco laboris nisi ut aliquip ex ea
1790 // commodo consequat.
1793 ## `match_arm_blocks`
1795 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1797 - **Default value**: `true`
1798 - **Possible values**: `true`, `false`
1800 #### `true` (default):
1805 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1807 false => println!("{}", sit),
1816 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1817 false => println!("{}", sit),
1821 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1825 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1827 - **Default value**: `"Overwrite"`
1828 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`