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):
51 let lorem = vec!["ipsum",
63 #### `"Block"` (default):
84 See also: [`control_brace_style`](#control_brace_style).
86 ### Function arguments
88 #### `"Block"` (default):
93 fn lorem(ipsum: usize) {}
113 fn lorem(ipsum: usize) {}
115 fn lorem(ipsum: usize,
128 #### `"Block"` (default):
162 #### `"Block"` (default):
170 Adipiscing: Eq = usize,
171 Consectetur: Eq = usize,
178 adipiscing: Adipiscing,
179 consectetur: Consectetur,
189 fn lorem<Ipsum: Eq = usize,
193 Adipiscing: Eq = usize,
194 Consectetur: Eq = usize,
200 adipiscing: Adipiscing,
201 consectetur: Consectetur,
210 #### `"Block"` (default):
225 let lorem = Lorem { ipsum: dolor,
230 See also: [`struct_lit_single_line`](#struct_lit_single_line), [`indent_style`](#indent_style).
234 #### `"Block"` (default):
237 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
251 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
261 ## `use_small_heuristics`
263 Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
265 - **Default value**: `true`
266 - **Possible values**: `true`, `false`
269 #### `true` (default):
275 Sit { amet: Consectetur, adipiscing: Elit },
293 let lorem = Lorem { ipsum: dolor };
295 let lorem = if ipsum { dolor } else { sit };
312 lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
319 let lorem = if ipsum {
329 Where to put a binary operator when a binary expression goes multiline.
331 - **Default value**: `"Front"`
332 - **Possible values**: `"Front"`, `"Back"`
335 #### `"Front"` (default):
339 let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo
340 || barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
342 let sum = 123456789012345678901234567890 + 123456789012345678901234567890
343 + 123456789012345678901234567890;
345 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
346 ..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
354 let or = foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo ||
355 barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar;
357 let sum = 123456789012345678901234567890 + 123456789012345678901234567890 +
358 123456789012345678901234567890;
360 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..
361 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
365 ## `combine_control_expr`
367 Combine control expressions with function calls.
369 - **Default value**: `true`
370 - **Possible values**: `true`, `false`
373 #### `true` (default):
385 foo!(if let Some(..) = x {
398 foo!(while let Some(..) = x {
432 if let Some(..) = x {
449 while let Some(..) = x {
475 Maximum length of comments. No effect unless`wrap_comments = true`.
477 - **Default value**: `80`
478 - **Possible values**: any positive integer
481 **Note:** A value of `0` results in [`wrap_comments`](#wrap_comments) being applied regardless of a line's width.
483 #### `80` (default; comments shorter than `comment_width`):
485 // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
488 #### `60` (comments longer than `comment_width`):
490 // Lorem ipsum dolor sit amet,
491 // consectetur adipiscing elit.
494 See also [`wrap_comments`](#wrap_comments).
496 ## `condense_wildcard_suffixes`
498 Replace strings of _ wildcards by a single .. in tuple patterns
500 - **Default value**: `false`
501 - **Possible values**: `true`, `false`
504 #### `false` (default):
508 let (lorem, ipsum, _, _) = (1, 2, 3, 4);
509 let (lorem, ipsum, ..) = (1, 2, 3, 4);
517 let (lorem, ipsum, ..) = (1, 2, 3, 4);
521 ## `control_brace_style`
523 Brace style for control flow constructs
525 - **Default value**: `"AlwaysSameLine"`
526 - **Possible values**: `"AlwaysNextLine"`, `"AlwaysSameLine"`, `"ClosingNextLine"`
529 #### `"AlwaysSameLine"` (default):
541 #### `"AlwaysNextLine"`:
556 #### `"ClosingNextLine"`:
569 ## `disable_all_formatting`
571 Don't reformat anything
573 - **Default value**: `false`
574 - **Possible values**: `true`, `false`
577 ## `error_on_line_overflow`
579 Error if unable to get all lines within `max_width`
581 - **Default value**: `true`
582 - **Possible values**: `true`, `false`
585 See also [`max_width`](#max_width).
587 ## `error_on_line_overflow_comments`
589 Error if unable to get all comment lines within `comment_width`.
591 - **Default value**: `true`
592 - **Possible values**: `true`, `false`
595 See also [`comment_width`](#comment_width).
599 Argument density in functions
601 - **Default value**: `"Tall"`
602 - **Possible values**: `"Compressed"`, `"Tall"`, `"Vertical"`
605 #### `"Tall"` (default):
609 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
611 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
620 consectetur: Consectetur,
621 adipiscing: Adipiscing,
630 consectetur: Consectetur,
631 adipiscing: Adipiscing,
643 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
645 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
650 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
651 adipiscing: Adipiscing, elit: Elit,
655 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
656 adipiscing: Adipiscing, elit: Elit,
688 consectetur: Consectetur,
689 adipiscing: Adipiscing,
698 consectetur: Consectetur,
699 adipiscing: Adipiscing,
710 Brace style for items
712 - **Default value**: `"SameLineWhere"`
713 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
718 #### `"SameLineWhere"` (default):
725 fn lorem(ipsum: usize) {
729 fn lorem<T>(ipsum: T)
731 T: Add + Sub + Mul + Div,
737 #### `"AlwaysNextLine"`:
745 fn lorem(ipsum: usize)
750 fn lorem<T>(ipsum: T)
752 T: Add + Sub + Mul + Div,
758 #### `"PreferSameLine"`:
765 fn lorem(ipsum: usize) {
769 fn lorem<T>(ipsum: T)
771 T: Add + Sub + Mul + Div, {
776 ### Structs and enums
778 #### `"SameLineWhere"` (default):
793 #### `"AlwaysNextLine"`:
809 #### `"PreferSameLine"`:
824 ## `empty_item_single_line`
826 Put empty-body functions and impls on a single line
828 - **Default value**: `true`
829 - **Possible values**: `true`, `false`
832 #### `true` (default):
850 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
855 Put single-expression functions on a single line
857 - **Default value**: `false`
858 - **Possible values**: `true`, `false`
861 #### `false` (default):
864 fn lorem() -> usize {
868 fn lorem() -> usize {
877 fn lorem() -> usize { 42 }
879 fn lorem() -> usize {
885 See also [`control_brace_style`](#control_brace_style).
888 ## `where_single_line`
890 To force single line where layout
892 - **Default value**: `false`
893 - **Possible values**: `true`, `false`
896 #### `false` (default):
911 where Option<T>: Ipsum
917 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
920 ## `force_explicit_abi`
922 Always print the abi for extern items
924 - **Default value**: `true`
925 - **Possible values**: `true`, `false`
928 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
930 #### `true` (default):
934 pub static lorem: c_int;
942 pub static lorem: c_int;
948 Format string literals where necessary
950 - **Default value**: `false`
951 - **Possible values**: `true`, `false`
954 #### `false` (default):
959 "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit amet consectetur adipiscing";
967 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit amet \
968 consectetur adipiscing";
972 See also [`max_width`](#max_width).
976 Use tab characters for indentation, spaces for alignment
978 - **Default value**: `false`
979 - **Possible values**: `true`, `false`
982 #### `false` (default):
985 fn lorem() -> usize {
986 42 // spaces before 42
993 fn lorem() -> usize {
998 See also: [`tab_spaces`](#tab_spaces).
1003 Indent style of imports
1005 - **Default Value**: `"Visual"`
1006 - **Possible values**: `"Block"`, `"Visual"`
1009 #### `"Visual"` (default):
1012 use foo::{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
1013 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz};
1020 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
1021 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
1025 See also: [`imports_layout`](#imports_layout).
1029 Item layout inside a imports block
1031 - **Default value**: "Mixed"
1032 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1035 #### `"Mixed"` (default):
1038 use foo::{xxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz};
1040 use foo::{aaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbb, cccccccccccccccccc, dddddddddddddddddd,
1041 eeeeeeeeeeeeeeeeee, ffffffffffffffffff};
1044 #### `"Horizontal"`:
1046 **Note**: This option forces all imports onto one line and may exceed `max_width`.
1049 use foo::{xxx, yyy, zzz};
1051 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1054 #### `"HorizontalVertical"`:
1057 use foo::{xxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz};
1059 use foo::{aaaaaaaaaaaaaaaaaa,
1064 ffffffffffffffffff};
1083 ## `match_block_trailing_comma`
1085 Put a trailing comma after a block based match arm (non-block arms are not affected)
1087 - **Default value**: `false`
1088 - **Possible values**: `true`, `false`
1091 #### `false` (default):
1099 Lorem::Dolor => println!("dolor"),
1112 Lorem::Dolor => println!("dolor"),
1117 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1121 Maximum width of each line
1123 - **Default value**: `100`
1124 - **Possible values**: any positive integer
1127 See also [`error_on_line_overflow`](#error_on_line_overflow).
1131 Merge multiple derives into a single one.
1133 - **Default value**: `true`
1134 - **Possible values**: `true`, `false`
1137 #### `true` (default):
1140 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1147 #[derive(Eq, PartialEq)]
1149 #[derive(Copy, Clone)]
1153 ## `force_multiline_blocks`
1155 Force multiline closure and match arm bodies to be wrapped in a block
1157 - **Default value**: `false`
1158 - **Possible values**: `false`, `true`
1161 #### `false` (default):
1165 result.and_then(|maybe_value| match maybe_value {
1167 Some(value) => bar(),
1172 println!("Hello World");
1174 Some(dolor) => foo(),
1183 result.and_then(|maybe_value| {
1186 Some(value) => bar(),
1193 println!("Hello World");
1196 Some(dolor) => foo(),
1204 Unix or Windows line endings
1206 - **Default value**: `"Unix"`
1207 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1210 ## `normalize_comments`
1212 Convert /* */ comments to // comments where possible
1214 - **Default value**: `false`
1215 - **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`
1246 #### `false` (default):
1249 use super::{lorem, ipsum, dolor, sit};
1255 use super::{dolor, ipsum, lorem, sit};
1258 See also [`reorder_imports`](#reorder_imports).
1260 ## `reorder_imports`
1262 Reorder import statements alphabetically
1264 - **Default value**: `false`
1265 - **Possible values**: `true`, `false`
1268 #### `false` (default):
1286 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1288 ## `reorder_imports_in_group`
1290 Reorder import statements in group
1292 - **Default value**: `false`
1293 - **Possible values**: `true`, `false`
1296 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1298 #### `false` (default):
1322 See also [`reorder_imports`](#reorder_imports).
1324 ## `reorder_extern_crates`
1326 Reorder `extern crate` statements alphabetically
1328 - **Default value**: `true`
1329 - **Possible values**: `true`, `false`
1332 #### `true` (default):
1350 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1352 ## `reorder_extern_crates_in_group`
1354 Reorder `extern crate` statements in group
1356 - **Default value**: `true`
1357 - **Possible values**: `true`, `false`
1360 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1362 #### `true` (default):
1386 See also [`reorder_extern_crates`](#reorder_extern_crates).
1390 Report `TODO` items in comments.
1392 - **Default value**: `"Never"`
1393 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1396 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1397 it contains a `#X` (with `X` being a number) in parentheses following the
1398 `TODO`, `"Unnumbered"` will ignore it.
1400 See also [`report_fixme`](#report_fixme).
1404 Report `FIXME` items in comments.
1406 - **Default value**: `"Never"`
1407 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1410 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1411 it contains a `#X` (with `X` being a number) in parentheses following the
1412 `FIXME`, `"Unnumbered"` will ignore it.
1414 See also [`report_todo`](#report_todo).
1419 Don't reformat out of line modules
1421 - **Default value**: `false`
1422 - **Possible values**: `true`, `false`
1425 ## `space_after_colon`
1427 Leave a space after the colon.
1429 - **Default value**: `true`
1430 - **Possible values**: `true`, `false`
1433 #### `true` (default):
1436 fn lorem<T: Eq>(t: T) {
1437 let lorem: Dolor = Lorem {
1447 fn lorem<T:Eq>(t:T) {
1448 let lorem:Dolor = Lorem {
1455 See also: [`space_before_colon`](#space_before_colon).
1457 ## `space_before_colon`
1459 Leave a space before the colon.
1461 - **Default value**: `false`
1462 - **Possible values**: `true`, `false`
1465 #### `false` (default):
1468 fn lorem<T: Eq>(t: T) {
1469 let lorem: Dolor = Lorem {
1479 fn lorem<T : Eq>(t : T) {
1480 let lorem : Dolor = Lorem {
1487 See also: [`space_after_colon`](#space_after_colon).
1489 ## `struct_field_align_threshold`
1491 The maximum diff of width between struct fields to be aligned with each other.
1493 - **Default value** : 0
1494 - **Possible values**: any positive integer
1517 ## `spaces_around_ranges`
1519 Put spaces around the .., ..=, and ... range operators
1521 - **Default value**: `false`
1522 - **Possible values**: `true`, `false`
1525 #### `false` (default):
1553 let lorem = 0 .. 10;
1554 let ipsum = 0 ..= 10;
1573 ## `spaces_within_parens_and_brackets`
1575 Put spaces within non-empty generic arguments, parentheses, and square brackets
1577 - **Default value**: `false`
1578 - **Possible values**: `true`, `false`
1581 #### `false` (default):
1584 // generic arguments
1585 fn lorem<T: Eq>(t: T) {
1589 // non-empty parentheses
1590 fn lorem<T: Eq>(t: T) {
1591 let lorem = (ipsum, dolor);
1594 // non-empty square brackets
1595 let lorem: [usize; 2] = [ipsum, dolor];
1601 // generic arguments
1602 fn lorem< T: Eq >(t: T) {
1606 // non-empty parentheses
1607 fn lorem<T: Eq>( t: T ) {
1608 let lorem = ( ipsum, dolor );
1611 // non-empty square brackets
1612 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1615 ## `struct_lit_single_line`
1617 Put small struct literals on a single line
1619 - **Default value**: `true`
1620 - **Possible values**: `true`, `false`
1623 #### `true` (default):
1626 let lorem = Lorem { ipsum: dolor, sit: amet };
1638 See also: [`indent_style`](#indent_style).
1643 Number of spaces per tab
1645 - **Default value**: `4`
1646 - **Possible values**: any positive integer
1653 let ipsum = dolor();
1655 "amet consectetur adipiscing elit."
1664 let ipsum = dolor();
1666 "amet consectetur adipiscing elit."
1671 See also: [`hard_tabs`](#hard_tabs).
1676 How to handle trailing commas for lists
1678 - **Default value**: `"Vertical"`
1679 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1682 #### `"Vertical"` (default):
1685 let Lorem { ipsum, dolor, sit } = amet;
1699 let Lorem { ipsum, dolor, sit, } = amet;
1713 let Lorem { ipsum, dolor, sit } = amet;
1724 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1726 ## `trailing_semicolon`
1728 Add trailing semicolon after break, continue and return
1730 - **Default value**: `true`
1731 - **Possible values**: `true`, `false`
1734 #### `true` (default):
1748 ## `type_punctuation_density`
1750 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1752 - **Default value**: `"Wide"`
1753 - **Possible values**: `"Compressed"`, `"Wide"`
1756 #### `"Wide"` (default):
1759 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1764 #### `"Compressed"`:
1767 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1772 ## `use_try_shorthand`
1774 Replace uses of the try! macro by the ? shorthand
1776 - **Default value**: `false`
1777 - **Possible values**: `true`, `false`
1780 #### `false` (default):
1783 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1789 let lorem = ipsum.map(|dolor| dolor.sit())?;
1795 Break comments to fit on the line
1797 - **Default value**: `false`
1798 - **Possible values**: `true`, `false`
1801 #### `false` (default):
1804 // 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.
1810 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1811 // sed do eiusmod tempor incididunt ut labore et dolore
1812 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1813 // exercitation ullamco laboris nisi ut aliquip ex ea
1814 // commodo consequat.
1817 ## `match_arm_blocks`
1819 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1821 - **Default value**: `true`
1822 - **Possible values**: `true`, `false`
1825 #### `true` (default):
1830 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1832 false => println!("{}", sit),
1841 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1842 false => println!("{}", sit),
1846 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1850 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1852 - **Default value**: `"Overwrite"`
1853 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`
1856 ## `blank_lines_upper_bound`
1858 Maximum number of blank lines which can be put between items. If more than this number of consecutive empty
1859 lines are found, they are trimmed down to match this integer.
1861 - **Default value**: `1`
1862 - **Possible values**: *unsigned integer*
1911 See also: [`blank_lines_lower_bound`](#blank_lines_lower_bound)
1913 ## `blank_lines_lower_bound`
1915 Minimum number of blank lines which must be put between items. If two items have fewer blank lines between
1916 them, additional blank lines are inserted.
1918 - **Default value**: `0`
1919 - **Possible values**: *unsigned integer*
1923 Original Code (rustfmt will not change it with the default value of `0`):