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).
909 ## `force_explicit_abi`
911 Always print the abi for extern items
913 - **Default value**: `true`
914 - **Possible values**: `true`, `false`
916 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
918 #### `true` (default):
922 pub static lorem: c_int;
930 pub static lorem: c_int;
936 Format string literals where necessary
938 - **Default value**: `false`
939 - **Possible values**: `true`, `false`
941 #### `false` (default):
944 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
951 "ipsum dolor sit amet consectetur \
952 adipiscing elit lorem ipsum dolor sit";
955 See also [`max_width`](#max_width).
959 Use tab characters for indentation, spaces for alignment
961 - **Default value**: `false`
962 - **Possible values**: `true`, `false`
964 #### `false` (default):
967 fn lorem() -> usize {
968 42 // spaces before 42
975 fn lorem() -> usize {
980 See also: [`tab_spaces`](#tab_spaces).
985 Indent style of imports
987 - **Default Value**: `"Visual"`
988 - **Possible values**: `"Block"`, `"Visual"`
990 #### `"Visual"` (default):
1008 See also: [`imports_layout`](#imports_layout).
1012 Item layout inside a imports block
1014 - **Default value**: "Mixed"
1015 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1017 #### `"Mixed"` (default):
1020 use foo::{xxx, yyy, zzz};
1022 use foo::{aaa, bbb, ccc,
1026 #### `"Horizontal"`:
1028 **Note**: This option forces to put everything on one line and may exceeds `max_width`.
1031 use foo::{xxx, yyy, zzz};
1033 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1036 #### `"HorizontalVertical"`:
1039 use foo::{xxx, yyy, zzz};
1065 ## `match_block_trailing_comma`
1067 Put a trailing comma after a block based match arm (non-block arms are not affected)
1069 - **Default value**: `false`
1070 - **Possible values**: `true`, `false`
1072 #### `false` (default):
1079 Lorem::Dolor => println!("dolor"),
1090 Lorem::Dolor => println!("dolor"),
1094 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1098 Maximum width of each line
1100 - **Default value**: `100`
1101 - **Possible values**: any positive integer
1103 See also [`error_on_line_overflow`](#error_on_line_overflow).
1107 Merge multiple derives into a single one.
1109 - **Default value**: `true`
1110 - **Possible values**: `true`, `false`
1112 #### `true` (default):
1115 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1122 #[derive(Eq, PartialEq)]
1124 #[derive(Copy, Clone)]
1128 ## `force_multiline_blocks`
1130 Force multiline closure and match arm bodies to be wrapped in a block
1132 - **Default value**: `false`
1133 - **Possible values**: `false`, `true`
1135 #### `false` (default):
1138 result.and_then(|maybe_value| match maybe_value {
1145 println!("Hello World");
1155 result.and_then(|maybe_value| {
1165 println!("Hello World");
1175 Unix or Windows line endings
1177 - **Default value**: `"Unix"`
1178 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1180 ## `normalize_comments`
1182 Convert /* */ comments to // comments where possible
1184 - **Default value**: `false`
1185 - **Possible values**: `true`, `false`
1187 #### `false` (default):
1191 fn dolor() -> usize {}
1194 fn adipiscing() -> usize {}
1201 fn dolor() -> usize {}
1204 fn adipiscing() -> usize {}
1207 ## `reorder_imported_names`
1209 Reorder lists of names in import statements alphabetically
1211 - **Default value**: `false`
1212 - **Possible values**: `true`, `false`
1214 #### `false` (default):
1217 use super::{lorem, ipsum, dolor, sit};
1223 use super::{dolor, ipsum, lorem, sit};
1226 See also [`reorder_imports`](#reorder_imports).
1228 ## `reorder_imports`
1230 Reorder import statements alphabetically
1232 - **Default value**: `false`
1233 - **Possible values**: `true`, `false`
1235 #### `false` (default):
1253 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1255 ## `reorder_imports_in_group`
1257 Reorder import statements in group
1259 - **Default value**: `false`
1260 - **Possible values**: `true`, `false`
1262 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1264 #### `false` (default):
1288 See also [`reorder_imports`](#reorder_imports).
1290 ## `reorder_extern_crates`
1292 Reorder `extern crate` statements alphabetically
1294 - **Default value**: `true`
1295 - **Possible values**: `true`, `false`
1297 #### `true` (default):
1315 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1317 ## `reorder_extern_crates_in_group`
1319 Reorder `extern crate` statements in group
1321 - **Default value**: `true`
1322 - **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"`
1359 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1360 it contains a `#X` (with `X` being a number) in parentheses following the
1361 `TODO`, `"Unnumbered"` will ignore it.
1363 See also [`report_fixme`](#report_fixme).
1367 Report `FIXME` items in comments.
1369 - **Default value**: `"Never"`
1370 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1372 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1373 it contains a `#X` (with `X` being a number) in parentheses following the
1374 `FIXME`, `"Unnumbered"` will ignore it.
1376 See also [`report_todo`](#report_todo).
1381 Don't reformat out of line modules
1383 - **Default value**: `false`
1384 - **Possible values**: `true`, `false`
1386 ## `space_after_colon`
1388 Leave a space after the colon.
1390 - **Default value**: `true`
1391 - **Possible values**: `true`, `false`
1393 #### `true` (default):
1396 fn lorem<T: Eq>(t: T) {
1397 let lorem: Dolor = Lorem {
1407 fn lorem<T:Eq>(t:T) {
1408 let lorem:Dolor = Lorem {
1415 See also: [`space_before_colon`](#space_before_colon).
1417 ## `space_before_colon`
1419 Leave a space before the colon.
1421 - **Default value**: `false`
1422 - **Possible values**: `true`, `false`
1424 #### `false` (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_after_colon`](#space_after_colon).
1448 ## `struct_field_align_threshold`
1450 The maximum diff of width between struct fields to be aligned with each other.
1452 - **Default value** : 0
1453 - **Possible values**: any positive integer
1477 ## `spaces_around_ranges`
1479 Put spaces around the .. and ... range operators
1481 - **Default value**: `false`
1482 - **Possible values**: `true`, `false`
1484 #### `false` (default):
1493 let lorem = 0 .. 10;
1496 ## `spaces_within_parens_and_brackets`
1498 Put spaces within non-empty generic arguments
1500 - **Default value**: `false`
1501 - **Possible values**: `true`, `false`
1503 #### `false` (default):
1506 fn lorem<T: Eq>(t: T) {
1514 fn lorem< T: Eq >(t: T) {
1519 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1521 ## `spaces_within_parens_and_brackets`
1523 Put spaces within non-empty parentheses
1525 - **Default value**: `false`
1526 - **Possible values**: `true`, `false`
1528 #### `false` (default):
1531 fn lorem<T: Eq>(t: T) {
1532 let lorem = (ipsum, dolor);
1539 fn lorem<T: Eq>( t: T ) {
1540 let lorem = ( ipsum, dolor );
1544 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1546 ## `spaces_within_parens_and_brackets`
1548 Put spaces within non-empty square brackets
1550 - **Default value**: `false`
1551 - **Possible values**: `true`, `false`
1553 #### `false` (default):
1556 let lorem: [usize; 2] = [ipsum, dolor];
1562 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1565 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1567 ## `struct_lit_single_line`
1569 Put small struct literals on a single line
1571 - **Default value**: `true`
1572 - **Possible values**: `true`, `false`
1574 #### `true` (default):
1577 let lorem = Lorem { ipsum: dolor, sit: amet };
1589 See also: [`indent_style`](#indent_style).
1594 Number of spaces per tab
1596 - **Default value**: `4`
1597 - **Possible values**: any positive integer
1603 let ipsum = dolor();
1605 "amet consectetur adipiscing elit."
1614 let ipsum = dolor();
1616 "amet consectetur adipiscing elit."
1621 See also: [`hard_tabs`](#hard_tabs).
1626 How to handle trailing commas for lists
1628 - **Default value**: `"Vertical"`
1629 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1631 #### `"Vertical"` (default):
1634 let Lorem { ipsum, dolor, sit } = amet;
1648 let Lorem { ipsum, dolor, sit, } = amet;
1662 let Lorem { ipsum, dolor, sit } = amet;
1673 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1675 ## `trailing_semicolon`
1677 Add trailing semicolon after break, continue and return
1679 - **Default value**: `true`
1680 - **Possible values**: `true`, `false`
1682 #### `true` (default):
1696 ## `type_punctuation_density`
1698 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1700 - **Default value**: `"Wide"`
1701 - **Possible values**: `"Compressed"`, `"Wide"`
1703 #### `"Wide"` (default):
1706 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1711 #### `"Compressed"`:
1714 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1719 ## `use_try_shorthand`
1721 Replace uses of the try! macro by the ? shorthand
1723 - **Default value**: `false`
1724 - **Possible values**: `true`, `false`
1726 #### `false` (default):
1729 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1735 let lorem = ipsum.map(|dolor| dolor.sit())?;
1741 Break comments to fit on the line
1743 - **Default value**: `false`
1744 - **Possible values**: `true`, `false`
1746 #### `false` (default):
1749 // 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.
1755 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1756 // sed do eiusmod tempor incididunt ut labore et dolore
1757 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1758 // exercitation ullamco laboris nisi ut aliquip ex ea
1759 // commodo consequat.
1762 ## `match_arm_blocks`
1764 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1766 - **Default value**: `true`
1767 - **Possible values**: `true`, `false`
1769 #### `true` (default):
1774 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1776 false => println!("{}", sit),
1785 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1786 false => println!("{}", sit),
1790 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1794 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1796 - **Default value**: `"Overwrite"`
1797 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`