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.
15 To enable unstable options, set `unstable_features = true` in `rustfmt.toml` or pass `--unstable-options` to rustfmt,
16 and ensure that the environment variable `CFG_RELEASE_CHANNEL` is set to `nightly`.
18 # Configuration Options
20 Below you find a detailed visual guide on all the supported configuration options of rustfmt:
25 Indent on expressions or items.
27 - **Default value**: `"Block"`
28 - **Possible values**: `"Block"`, `"Visual"`
33 #### `"Block"` (default):
50 let lorem = vec!["ipsum",
61 #### `"Block"` (default):
82 See also: [`control_brace_style`](#control_brace_style).
84 ### Function arguments
86 #### `"Block"` (default):
91 fn lorem(ipsum: usize) {}
111 fn lorem(ipsum: usize) {}
113 fn lorem(ipsum: usize,
126 #### `"Block"` (default):
156 #### `"Block"` (default):
164 Adipiscing: Eq = usize,
165 Consectetur: Eq = usize,
172 adipiscing: Adipiscing,
173 consectetur: Consectetur,
183 fn lorem<Ipsum: Eq = usize,
187 Adipiscing: Eq = usize,
188 Consectetur: Eq = usize,
194 adipiscing: Adipiscing,
195 consectetur: Consectetur,
204 #### `"Block"` (default):
216 let lorem = Lorem { ipsum: dolor,
220 See also: [`struct_lit_single_line`](#struct_lit_single_line), [`indent_style`](#indent_style).
224 #### `"Block"` (default):
227 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
241 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
252 ## `same_line_attributes`
254 Try to put attributes on the same line as fields and variants
256 - **Default value**: `true`
257 - **Possible values**: `true`, `false`
260 #### `true` (default):
264 #[serde(rename = "Ipsum")] ipsum: usize,
265 #[serde(rename = "Dolor")] dolor: usize,
266 #[serde(rename = "Amet")] amet: usize,
270 #[serde(skip_serializing)] Ipsum,
271 #[serde(skip_serializing)] Dolor,
272 #[serde(skip_serializing)] Amet,
280 #[serde(rename = "Ipsum")]
282 #[serde(rename = "Dolor")]
284 #[serde(rename = "Amet")]
289 #[serde(skip_serializing)]
291 #[serde(skip_serializing)]
293 #[serde(skip_serializing)]
298 ## `use_small_heuristics`
300 Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
302 - **Default value**: `true`
303 - **Possible values**: `true`, `false`
306 #### `true` (default):
312 Sit { amet: Consectetur, adipiscing: Elit },
326 let lorem = Lorem { ipsum: dolor, sit: amet };
328 let lorem = if ipsum { dolor } else { sit };
345 lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
352 let lorem = if ipsum {
362 Where to put a binary operator when a binary expression goes multiline.
364 - **Default value**: `"Front"`
365 - **Possible values**: `"Front"`, `"Back"`
368 #### `"Front"` (default):
379 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
380 ..bbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
394 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..
395 bbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
402 - **Default value**: `"Block"`
403 - **Possible values**: `"Block"`, `"Visual"`
406 #### `"Block"` (default):
421 let lorem = ipsum.dolor()
431 ## `combine_control_expr`
433 Combine control expressions with function calls.
435 - **Default value**: `true`
436 - **Possible values**: `true`, `false`
439 #### `true` (default):
451 foo!(if let Some(..) = x {
464 foo!(while let Some(..) = x {
498 if let Some(..) = x {
515 while let Some(..) = x {
541 Maximum length of comments. No effect unless`wrap_comments = true`.
543 - **Default value**: `80`
544 - **Possible values**: any positive integer
547 **Note:** A value of `0` results in [`wrap_comments`](#wrap_comments) being applied regardless of a line's width.
549 #### Comments shorter than `comment_width`:
551 // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
554 #### Comments longer than `comment_width`:
556 // Lorem ipsum dolor sit amet,
557 // consectetur adipiscing elit.
560 See also [`wrap_comments`](#wrap_comments).
562 ## `condense_wildcard_suffixes`
564 Replace strings of _ wildcards by a single .. in tuple patterns
566 - **Default value**: `false`
567 - **Possible values**: `true`, `false`
570 #### `false` (default):
573 let (lorem, ipsum, _, _) = (1, 2, 3, 4);
579 let (lorem, ipsum, ..) = (1, 2, 3, 4);
582 ## `control_brace_style`
584 Brace style for control flow constructs
586 - **Default value**: `"AlwaysSameLine"`
587 - **Possible values**: `"AlwaysNextLine"`, `"AlwaysSameLine"`, `"ClosingNextLine"`
590 #### `"AlwaysSameLine"` (default):
600 #### `"AlwaysNextLine"`:
613 #### `"ClosingNextLine"`:
624 ## `disable_all_formatting`
626 Don't reformat anything
628 - **Default value**: `false`
629 - **Possible values**: `true`, `false`
632 ## `error_on_line_overflow`
634 Error if unable to get all lines within `max_width`
636 - **Default value**: `true`
637 - **Possible values**: `true`, `false`
640 See also [`max_width`](#max_width).
642 ## `error_on_line_overflow_comments`
644 Error if unable to get all comment lines within `comment_width`.
646 - **Default value**: `true`
647 - **Possible values**: `true`, `false`
650 See also [`comment_width`](#comment_width).
654 Argument density in functions
656 - **Default value**: `"Tall"`
657 - **Possible values**: `"Compressed"`, `"Tall"`, `"Vertical"`
660 #### `"Tall"` (default):
664 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
666 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
675 consectetur: Consectetur,
676 adipiscing: Adipiscing,
685 consectetur: Consectetur,
686 adipiscing: Adipiscing,
698 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
700 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
705 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
706 adipiscing: Adipiscing, elit: Elit,
710 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
711 adipiscing: Adipiscing, elit: Elit,
722 fn lorem(ipsum: Ipsum,
727 fn lorem(ipsum: Ipsum,
734 fn lorem(ipsum: Ipsum,
738 consectetur: Consectetur,
739 adipiscing: Adipiscing,
742 fn lorem(ipsum: Ipsum,
746 consectetur: Consectetur,
747 adipiscing: Adipiscing,
757 Brace style for items
759 - **Default value**: `"SameLineWhere"`
760 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
765 #### `"SameLineWhere"` (default):
772 fn lorem(ipsum: usize) {
776 fn lorem<T>(ipsum: T)
778 T: Add + Sub + Mul + Div,
784 #### `"AlwaysNextLine"`:
792 fn lorem(ipsum: usize)
797 fn lorem<T>(ipsum: T)
799 T: Add + Sub + Mul + Div,
805 #### `"PreferSameLine"`:
812 fn lorem(ipsum: usize) {
816 fn lorem<T>(ipsum: T)
818 T: Add + Sub + Mul + Div, {
823 ### Structs and enums
825 #### `"SameLineWhere"` (default):
839 #### `"AlwaysNextLine"`:
854 #### `"PreferSameLine"`:
868 ## `empty_item_single_line`
870 Put empty-body functions and impls on a single line
872 - **Default value**: `true`
873 - **Possible values**: `true`, `false`
876 #### `true` (default):
894 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
899 Put single-expression functions on a single line
901 - **Default value**: `false`
902 - **Possible values**: `true`, `false`
905 #### `false` (default):
908 fn lorem() -> usize {
912 fn lorem() -> usize {
921 fn lorem() -> usize { 42 }
923 fn lorem() -> usize {
929 See also [`control_brace_style`](#control_brace_style).
932 ## `where_single_line`
934 To force single line where layout
936 - **Default value**: `false`
937 - **Possible values**: `true`, `false`
940 #### `false` (default):
955 where Option<T>: Ipsum {
960 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
963 ## `force_explicit_abi`
965 Always print the abi for extern items
967 - **Default value**: `true`
968 - **Possible values**: `true`, `false`
971 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
973 #### `true` (default):
977 pub static lorem: c_int;
985 pub static lorem: c_int;
991 Format string literals where necessary
993 - **Default value**: `false`
994 - **Possible values**: `true`, `false`
997 #### `false` (default):
1000 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
1007 "ipsum dolor sit amet consectetur \
1008 adipiscing elit lorem ipsum dolor sit";
1011 See also [`max_width`](#max_width).
1015 Use tab characters for indentation, spaces for alignment
1017 - **Default value**: `false`
1018 - **Possible values**: `true`, `false`
1021 #### `false` (default):
1024 fn lorem() -> usize {
1025 42 // spaces before 42
1032 fn lorem() -> usize {
1033 42 // tabs before 42
1037 See also: [`tab_spaces`](#tab_spaces).
1042 Indent style of imports
1044 - **Default Value**: `"Visual"`
1045 - **Possible values**: `"Block"`, `"Visual"`
1048 #### `"Visual"` (default):
1066 See also: [`imports_layout`](#imports_layout).
1070 Item layout inside a imports block
1072 - **Default value**: "Mixed"
1073 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1076 #### `"Mixed"` (default):
1079 use foo::{xxx, yyy, zzz};
1081 use foo::{aaa, bbb, ccc,
1085 #### `"Horizontal"`:
1087 **Note**: This option forces to put everything on one line and may exceeds `max_width`.
1090 use foo::{xxx, yyy, zzz};
1092 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1095 #### `"HorizontalVertical"`:
1098 use foo::{xxx, yyy, zzz};
1124 ## `match_block_trailing_comma`
1126 Put a trailing comma after a block based match arm (non-block arms are not affected)
1128 - **Default value**: `false`
1129 - **Possible values**: `true`, `false`
1132 #### `false` (default):
1139 Lorem::Dolor => println!("dolor"),
1150 Lorem::Dolor => println!("dolor"),
1154 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1158 Maximum width of each line
1160 - **Default value**: `100`
1161 - **Possible values**: any positive integer
1164 See also [`error_on_line_overflow`](#error_on_line_overflow).
1168 Merge multiple derives into a single one.
1170 - **Default value**: `true`
1171 - **Possible values**: `true`, `false`
1174 #### `true` (default):
1177 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1184 #[derive(Eq, PartialEq)]
1186 #[derive(Copy, Clone)]
1190 ## `force_multiline_blocks`
1192 Force multiline closure and match arm bodies to be wrapped in a block
1194 - **Default value**: `false`
1195 - **Possible values**: `false`, `true`
1198 #### `false` (default):
1201 result.and_then(|maybe_value| match maybe_value {
1208 println!("Hello World");
1218 result.and_then(|maybe_value| {
1228 println!("Hello World");
1238 Unix or Windows line endings
1240 - **Default value**: `"Unix"`
1241 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1244 ## `normalize_comments`
1246 Convert /* */ comments to // comments where possible
1248 - **Default value**: `false`
1249 - **Possible values**: `true`, `false`
1252 #### `false` (default):
1256 fn dolor() -> usize {}
1259 fn adipiscing() -> usize {}
1266 fn dolor() -> usize {}
1269 fn adipiscing() -> usize {}
1272 ## `reorder_imported_names`
1274 Reorder lists of names in import statements alphabetically
1276 - **Default value**: `false`
1277 - **Possible values**: `true`, `false`
1280 #### `false` (default):
1283 use super::{lorem, ipsum, dolor, sit};
1289 use super::{dolor, ipsum, lorem, sit};
1292 See also [`reorder_imports`](#reorder_imports).
1294 ## `reorder_imports`
1296 Reorder import statements alphabetically
1298 - **Default value**: `false`
1299 - **Possible values**: `true`, `false`
1302 #### `false` (default):
1320 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1322 ## `reorder_imports_in_group`
1324 Reorder import statements in group
1326 - **Default value**: `false`
1327 - **Possible values**: `true`, `false`
1330 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1332 #### `false` (default):
1356 See also [`reorder_imports`](#reorder_imports).
1358 ## `reorder_extern_crates`
1360 Reorder `extern crate` statements alphabetically
1362 - **Default value**: `true`
1363 - **Possible values**: `true`, `false`
1366 #### `true` (default):
1384 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1386 ## `reorder_extern_crates_in_group`
1388 Reorder `extern crate` statements in group
1390 - **Default value**: `true`
1391 - **Possible values**: `true`, `false`
1394 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1396 #### `true` (default):
1420 See also [`reorder_extern_crates`](#reorder_extern_crates).
1424 Report `TODO` items in comments.
1426 - **Default value**: `"Never"`
1427 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1430 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1431 it contains a `#X` (with `X` being a number) in parentheses following the
1432 `TODO`, `"Unnumbered"` will ignore it.
1434 See also [`report_fixme`](#report_fixme).
1438 Report `FIXME` items in comments.
1440 - **Default value**: `"Never"`
1441 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1444 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1445 it contains a `#X` (with `X` being a number) in parentheses following the
1446 `FIXME`, `"Unnumbered"` will ignore it.
1448 See also [`report_todo`](#report_todo).
1453 Don't reformat out of line modules
1455 - **Default value**: `false`
1456 - **Possible values**: `true`, `false`
1459 ## `space_after_colon`
1461 Leave a space after the colon.
1463 - **Default value**: `true`
1464 - **Possible values**: `true`, `false`
1467 #### `true` (default):
1470 fn lorem<T: Eq>(t: T) {
1471 let lorem: Dolor = Lorem {
1481 fn lorem<T:Eq>(t:T) {
1482 let lorem:Dolor = Lorem {
1489 See also: [`space_before_colon`](#space_before_colon).
1491 ## `space_before_colon`
1493 Leave a space before the colon.
1495 - **Default value**: `false`
1496 - **Possible values**: `true`, `false`
1499 #### `false` (default):
1502 fn lorem<T: Eq>(t: T) {
1503 let lorem: Dolor = Lorem {
1513 fn lorem<T : Eq>(t : T) {
1514 let lorem : Dolor = Lorem {
1521 See also: [`space_after_colon`](#space_after_colon).
1523 ## `struct_field_align_threshold`
1525 The maximum diff of width between struct fields to be aligned with each other.
1527 - **Default value** : 0
1528 - **Possible values**: any positive integer
1551 ## `spaces_around_ranges`
1553 Put spaces around the .. and ... range operators
1555 - **Default value**: `false`
1556 - **Possible values**: `true`, `false`
1559 #### `false` (default):
1568 let lorem = 0 .. 10;
1571 ## `spaces_within_parens_and_brackets`
1573 Put spaces within non-empty generic arguments, parentheses, and square brackets
1575 - **Default value**: `false`
1576 - **Possible values**: `true`, `false`
1579 #### `false` (default):
1582 // generic arguments
1583 fn lorem<T: Eq>(t: T) {
1587 // non-empty parentheses
1588 fn lorem<T: Eq>(t: T) {
1589 let lorem = (ipsum, dolor);
1592 // non-empty square brackets
1593 let lorem: [usize; 2] = [ipsum, dolor];
1599 // generic arguments
1600 fn lorem< T: Eq >(t: T) {
1604 // non-empty parentheses
1605 fn lorem<T: Eq>( t: T ) {
1606 let lorem = ( ipsum, dolor );
1609 // non-empty square brackets
1610 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1613 ## `struct_lit_single_line`
1615 Put small struct literals on a single line
1617 - **Default value**: `true`
1618 - **Possible values**: `true`, `false`
1621 #### `true` (default):
1624 let lorem = Lorem { ipsum: dolor, sit: amet };
1636 See also: [`indent_style`](#indent_style).
1641 Number of spaces per tab
1643 - **Default value**: `4`
1644 - **Possible values**: any positive integer
1651 let ipsum = dolor();
1653 "amet consectetur adipiscing elit."
1662 let ipsum = dolor();
1664 "amet consectetur adipiscing elit."
1669 See also: [`hard_tabs`](#hard_tabs).
1674 How to handle trailing commas for lists
1676 - **Default value**: `"Vertical"`
1677 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1680 #### `"Vertical"` (default):
1683 let Lorem { ipsum, dolor, sit } = amet;
1697 let Lorem { ipsum, dolor, sit, } = amet;
1711 let Lorem { ipsum, dolor, sit } = amet;
1722 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1724 ## `trailing_semicolon`
1726 Add trailing semicolon after break, continue and return
1728 - **Default value**: `true`
1729 - **Possible values**: `true`, `false`
1732 #### `true` (default):
1746 ## `type_punctuation_density`
1748 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1750 - **Default value**: `"Wide"`
1751 - **Possible values**: `"Compressed"`, `"Wide"`
1754 #### `"Wide"` (default):
1757 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1762 #### `"Compressed"`:
1765 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1770 ## `use_try_shorthand`
1772 Replace uses of the try! macro by the ? shorthand
1774 - **Default value**: `false`
1775 - **Possible values**: `true`, `false`
1778 #### `false` (default):
1781 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1787 let lorem = ipsum.map(|dolor| dolor.sit())?;
1793 Break comments to fit on the line
1795 - **Default value**: `false`
1796 - **Possible values**: `true`, `false`
1799 #### `false` (default):
1802 // 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.
1808 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1809 // sed do eiusmod tempor incididunt ut labore et dolore
1810 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1811 // exercitation ullamco laboris nisi ut aliquip ex ea
1812 // commodo consequat.
1815 ## `match_arm_blocks`
1817 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1819 - **Default value**: `true`
1820 - **Possible values**: `true`, `false`
1823 #### `true` (default):
1828 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1830 false => println!("{}", sit),
1839 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1840 false => println!("{}", sit),
1844 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1848 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1850 - **Default value**: `"Overwrite"`
1851 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`