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 {
916 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
919 ## `force_explicit_abi`
921 Always print the abi for extern items
923 - **Default value**: `true`
924 - **Possible values**: `true`, `false`
927 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
929 #### `true` (default):
933 pub static lorem: c_int;
941 pub static lorem: c_int;
947 Format string literals where necessary
949 - **Default value**: `false`
950 - **Possible values**: `true`, `false`
953 #### `false` (default):
956 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
963 "ipsum dolor sit amet consectetur \
964 adipiscing elit lorem ipsum dolor sit";
967 See also [`max_width`](#max_width).
971 Use tab characters for indentation, spaces for alignment
973 - **Default value**: `false`
974 - **Possible values**: `true`, `false`
977 #### `false` (default):
980 fn lorem() -> usize {
981 42 // spaces before 42
988 fn lorem() -> usize {
993 See also: [`tab_spaces`](#tab_spaces).
998 Indent style of imports
1000 - **Default Value**: `"Visual"`
1001 - **Possible values**: `"Block"`, `"Visual"`
1004 #### `"Visual"` (default):
1022 See also: [`imports_layout`](#imports_layout).
1026 Item layout inside a imports block
1028 - **Default value**: "Mixed"
1029 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1032 #### `"Mixed"` (default):
1035 use foo::{xxx, yyy, zzz};
1037 use foo::{aaa, bbb, ccc,
1041 #### `"Horizontal"`:
1043 **Note**: This option forces all imports onto one line and may exceed `max_width`.
1046 use foo::{xxx, yyy, zzz};
1048 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1051 #### `"HorizontalVertical"`:
1054 use foo::{xxx, yyy, zzz};
1080 ## `match_block_trailing_comma`
1082 Put a trailing comma after a block based match arm (non-block arms are not affected)
1084 - **Default value**: `false`
1085 - **Possible values**: `true`, `false`
1088 #### `false` (default):
1095 Lorem::Dolor => println!("dolor"),
1106 Lorem::Dolor => println!("dolor"),
1110 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1114 Maximum width of each line
1116 - **Default value**: `100`
1117 - **Possible values**: any positive integer
1120 See also [`error_on_line_overflow`](#error_on_line_overflow).
1124 Merge multiple derives into a single one.
1126 - **Default value**: `true`
1127 - **Possible values**: `true`, `false`
1130 #### `true` (default):
1133 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1140 #[derive(Eq, PartialEq)]
1142 #[derive(Copy, Clone)]
1146 ## `force_multiline_blocks`
1148 Force multiline closure and match arm bodies to be wrapped in a block
1150 - **Default value**: `false`
1151 - **Possible values**: `false`, `true`
1154 #### `false` (default):
1157 result.and_then(|maybe_value| match maybe_value {
1164 println!("Hello World");
1174 result.and_then(|maybe_value| {
1184 println!("Hello World");
1194 Unix or Windows line endings
1196 - **Default value**: `"Unix"`
1197 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1200 ## `normalize_comments`
1202 Convert /* */ comments to // comments where possible
1204 - **Default value**: `false`
1205 - **Possible values**: `true`, `false`
1208 #### `false` (default):
1212 fn dolor() -> usize {}
1215 fn adipiscing() -> usize {}
1222 fn dolor() -> usize {}
1225 fn adipiscing() -> usize {}
1228 ## `reorder_imported_names`
1230 Reorder lists of names in import statements alphabetically
1232 - **Default value**: `false`
1233 - **Possible values**: `true`, `false`
1236 #### `false` (default):
1239 use super::{lorem, ipsum, dolor, sit};
1245 use super::{dolor, ipsum, lorem, sit};
1248 See also [`reorder_imports`](#reorder_imports).
1250 ## `reorder_imports`
1252 Reorder import statements alphabetically
1254 - **Default value**: `false`
1255 - **Possible values**: `true`, `false`
1258 #### `false` (default):
1276 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1278 ## `reorder_imports_in_group`
1280 Reorder import statements in group
1282 - **Default value**: `false`
1283 - **Possible values**: `true`, `false`
1286 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1288 #### `false` (default):
1312 See also [`reorder_imports`](#reorder_imports).
1314 ## `reorder_extern_crates`
1316 Reorder `extern crate` statements alphabetically
1318 - **Default value**: `true`
1319 - **Possible values**: `true`, `false`
1322 #### `true` (default):
1340 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1342 ## `reorder_extern_crates_in_group`
1344 Reorder `extern crate` statements in group
1346 - **Default value**: `true`
1347 - **Possible values**: `true`, `false`
1350 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1352 #### `true` (default):
1376 See also [`reorder_extern_crates`](#reorder_extern_crates).
1380 Report `TODO` items in comments.
1382 - **Default value**: `"Never"`
1383 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1386 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1387 it contains a `#X` (with `X` being a number) in parentheses following the
1388 `TODO`, `"Unnumbered"` will ignore it.
1390 See also [`report_fixme`](#report_fixme).
1394 Report `FIXME` items in comments.
1396 - **Default value**: `"Never"`
1397 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1400 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1401 it contains a `#X` (with `X` being a number) in parentheses following the
1402 `FIXME`, `"Unnumbered"` will ignore it.
1404 See also [`report_todo`](#report_todo).
1409 Don't reformat out of line modules
1411 - **Default value**: `false`
1412 - **Possible values**: `true`, `false`
1415 ## `space_after_colon`
1417 Leave a space after the colon.
1419 - **Default value**: `true`
1420 - **Possible values**: `true`, `false`
1423 #### `true` (default):
1426 fn lorem<T: Eq>(t: T) {
1427 let lorem: Dolor = Lorem {
1437 fn lorem<T:Eq>(t:T) {
1438 let lorem:Dolor = Lorem {
1445 See also: [`space_before_colon`](#space_before_colon).
1447 ## `space_before_colon`
1449 Leave a space before the colon.
1451 - **Default value**: `false`
1452 - **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
1507 ## `spaces_around_ranges`
1509 Put spaces around the .. and ... range operators
1511 - **Default value**: `false`
1512 - **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, parentheses, and square brackets
1531 - **Default value**: `false`
1532 - **Possible values**: `true`, `false`
1535 #### `false` (default):
1538 // generic arguments
1539 fn lorem<T: Eq>(t: T) {
1543 // non-empty parentheses
1544 fn lorem<T: Eq>(t: T) {
1545 let lorem = (ipsum, dolor);
1548 // non-empty square brackets
1549 let lorem: [usize; 2] = [ipsum, dolor];
1555 // generic arguments
1556 fn lorem< T: Eq >(t: T) {
1560 // non-empty parentheses
1561 fn lorem<T: Eq>( t: T ) {
1562 let lorem = ( ipsum, dolor );
1565 // non-empty square brackets
1566 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1569 ## `struct_lit_single_line`
1571 Put small struct literals on a single line
1573 - **Default value**: `true`
1574 - **Possible values**: `true`, `false`
1577 #### `true` (default):
1580 let lorem = Lorem { ipsum: dolor, sit: amet };
1592 See also: [`indent_style`](#indent_style).
1597 Number of spaces per tab
1599 - **Default value**: `4`
1600 - **Possible values**: any positive integer
1607 let ipsum = dolor();
1609 "amet consectetur adipiscing elit."
1618 let ipsum = dolor();
1620 "amet consectetur adipiscing elit."
1625 See also: [`hard_tabs`](#hard_tabs).
1630 How to handle trailing commas for lists
1632 - **Default value**: `"Vertical"`
1633 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1636 #### `"Vertical"` (default):
1639 let Lorem { ipsum, dolor, sit } = amet;
1653 let Lorem { ipsum, dolor, sit, } = amet;
1667 let Lorem { ipsum, dolor, sit } = amet;
1678 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1680 ## `trailing_semicolon`
1682 Add trailing semicolon after break, continue and return
1684 - **Default value**: `true`
1685 - **Possible values**: `true`, `false`
1688 #### `true` (default):
1702 ## `type_punctuation_density`
1704 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1706 - **Default value**: `"Wide"`
1707 - **Possible values**: `"Compressed"`, `"Wide"`
1710 #### `"Wide"` (default):
1713 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1718 #### `"Compressed"`:
1721 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1726 ## `use_try_shorthand`
1728 Replace uses of the try! macro by the ? shorthand
1730 - **Default value**: `false`
1731 - **Possible values**: `true`, `false`
1734 #### `false` (default):
1737 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1743 let lorem = ipsum.map(|dolor| dolor.sit())?;
1749 Break comments to fit on the line
1751 - **Default value**: `false`
1752 - **Possible values**: `true`, `false`
1755 #### `false` (default):
1758 // 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.
1764 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1765 // sed do eiusmod tempor incididunt ut labore et dolore
1766 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1767 // exercitation ullamco laboris nisi ut aliquip ex ea
1768 // commodo consequat.
1771 ## `match_arm_blocks`
1773 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1775 - **Default value**: `true`
1776 - **Possible values**: `true`, `false`
1779 #### `true` (default):
1784 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1786 false => println!("{}", sit),
1795 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1796 false => println!("{}", sit),
1800 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1804 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1806 - **Default value**: `"Overwrite"`
1807 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`
1810 ## `blank_lines_upper_bound`
1812 Maximum number of blank lines which can be put between items. If more than this number of consecutive empty
1813 lines are found, they are trimmed down to match this integer.
1815 - **Default value**: `1`
1816 - **Possible values**: *unsigned integer*
1865 See also: [`blank_lines_lower_bound`](#blank_lines_lower_bound)
1867 ## `blank_lines_lower_bound`
1869 Minimum number of blank lines which must be put between items. If two items have fewer blank lines between
1870 them, additional blank lines are inserted.
1872 - **Default value**: `0`
1873 - **Possible values**: *unsigned integer*
1877 Original Code (rustfmt will not change it with the default value of `0`):