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"`, `"CompressedIfEmpty"`, `"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,
699 #### `"CompressedIfEmpty"`:
703 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
705 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
710 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
711 adipiscing: Adipiscing, elit: Elit,
719 consectetur: Consectetur,
720 adipiscing: Adipiscing,
732 fn lorem(ipsum: Ipsum,
737 fn lorem(ipsum: Ipsum,
744 fn lorem(ipsum: Ipsum,
748 consectetur: Consectetur,
749 adipiscing: Adipiscing,
752 fn lorem(ipsum: Ipsum,
756 consectetur: Consectetur,
757 adipiscing: Adipiscing,
767 Brace style for items
769 - **Default value**: `"SameLineWhere"`
770 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
774 #### `"SameLineWhere"` (default):
781 fn lorem(ipsum: usize) {
785 fn lorem<T>(ipsum: T)
787 T: Add + Sub + Mul + Div,
793 #### `"AlwaysNextLine"`:
801 fn lorem(ipsum: usize)
806 fn lorem<T>(ipsum: T)
808 T: Add + Sub + Mul + Div,
814 #### `"PreferSameLine"`:
821 fn lorem(ipsum: usize) {
825 fn lorem<T>(ipsum: T)
827 T: Add + Sub + Mul + Div, {
832 ### Structs and enums
834 #### `"SameLineWhere"` (default):
848 #### `"AlwaysNextLine"`:
863 #### `"PreferSameLine"`:
878 ## `fn_empty_single_line`
880 Put empty-body functions on a single line
882 - **Default value**: `true`
883 - **Possible values**: `true`, `false`
885 #### `true` (default):
898 See also [`control_brace_style`](#control_brace_style).
903 Put single-expression functions on a single line
905 - **Default value**: `false`
906 - **Possible values**: `true`, `false`
908 #### `false` (default):
911 fn lorem() -> usize {
915 fn lorem() -> usize {
924 fn lorem() -> usize { 42 }
926 fn lorem() -> usize {
932 See also [`control_brace_style`](#control_brace_style).
934 ## `force_explicit_abi`
936 Always print the abi for extern items
938 - **Default value**: `true`
939 - **Possible values**: `true`, `false`
941 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
943 #### `true` (default):
947 pub static lorem: c_int;
955 pub static lorem: c_int;
961 Format string literals where necessary
963 - **Default value**: `false`
964 - **Possible values**: `true`, `false`
966 #### `false` (default):
969 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
976 "ipsum dolor sit amet consectetur \
977 adipiscing elit lorem ipsum dolor sit";
980 See also [`max_width`](#max_width).
984 Use tab characters for indentation, spaces for alignment
986 - **Default value**: `false`
987 - **Possible values**: `true`, `false`
989 #### `false` (default):
992 fn lorem() -> usize {
993 42 // spaces before 42
1000 fn lorem() -> usize {
1001 42 // tabs before 42
1005 See also: [`tab_spaces`](#tab_spaces).
1007 ## `impl_empty_single_line`
1009 Put empty-body implementations on a single line
1011 - **Default value**: `true`
1012 - **Possible values**: `true`, `false`
1014 #### `true` (default):
1027 See also [`brace_style`](#brace_style).
1032 Indent style of imports
1034 - **Default Value**: `"Visual"`
1035 - **Possible values**: `"Block"`, `"Visual"`
1037 #### `"Visual"` (default):
1055 See also: [`imports_layout`](#imports_layout).
1059 Item layout inside a imports block
1061 - **Default value**: "Mixed"
1062 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1064 #### `"Mixed"` (default):
1067 use foo::{xxx, yyy, zzz};
1069 use foo::{aaa, bbb, ccc,
1073 #### `"Horizontal"`:
1075 **Note**: This option forces to put everything on one line and may exceeds `max_width`.
1078 use foo::{xxx, yyy, zzz};
1080 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1083 #### `"HorizontalVertical"`:
1086 use foo::{xxx, yyy, zzz};
1112 ## `match_block_trailing_comma`
1114 Put a trailing comma after a block based match arm (non-block arms are not affected)
1116 - **Default value**: `false`
1117 - **Possible values**: `true`, `false`
1119 #### `false` (default):
1126 Lorem::Dolor => println!("dolor"),
1137 Lorem::Dolor => println!("dolor"),
1141 See also: [`trailing_comma`](#trailing_comma), [`wrap_match_arms`](#wrap_match_arms).
1145 Maximum width of each line
1147 - **Default value**: `100`
1148 - **Possible values**: any positive integer
1150 See also [`error_on_line_overflow`](#error_on_line_overflow).
1154 Merge multiple derives into a single one.
1156 - **Default value**: `true`
1157 - **Possible values**: `true`, `false`
1159 #### `true` (default):
1162 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1169 #[derive(Eq, PartialEq)]
1171 #[derive(Copy, Clone)]
1175 ## `force_multiline_blocks`
1177 Force multiline closure and match arm bodies to be wrapped in a block
1179 - **Default value**: `false`
1180 - **Possible values**: `false`, `true`
1182 #### `false` (default):
1185 result.and_then(|maybe_value| match maybe_value {
1192 println!("Hello World");
1202 result.and_then(|maybe_value| {
1212 println!("Hello World");
1222 Unix or Windows line endings
1224 - **Default value**: `"Unix"`
1225 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1227 ## `normalize_comments`
1229 Convert /* */ comments to // comments where possible
1231 - **Default value**: `false`
1232 - **Possible values**: `true`, `false`
1234 #### `false` (default):
1238 fn dolor() -> usize {}
1241 fn adipiscing() -> usize {}
1248 fn dolor() -> usize {}
1251 fn adipiscing() -> usize {}
1254 ## `reorder_imported_names`
1256 Reorder lists of names in import statements alphabetically
1258 - **Default value**: `false`
1259 - **Possible values**: `true`, `false`
1261 #### `false` (default):
1264 use super::{lorem, ipsum, dolor, sit};
1270 use super::{dolor, ipsum, lorem, sit};
1273 See also [`reorder_imports`](#reorder_imports).
1275 ## `reorder_imports`
1277 Reorder import statements alphabetically
1279 - **Default value**: `false`
1280 - **Possible values**: `true`, `false`
1282 #### `false` (default):
1300 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1302 ## `reorder_imports_in_group`
1304 Reorder import statements in group
1306 - **Default value**: `false`
1307 - **Possible values**: `true`, `false`
1309 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1311 #### `false` (default):
1335 See also [`reorder_imports`](#reorder_imports).
1337 ## `reorder_extern_crates`
1339 Reorder `extern crate` statements alphabetically
1341 - **Default value**: `true`
1342 - **Possible values**: `true`, `false`
1344 #### `true` (default):
1362 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1364 ## `reorder_extern_crates_in_group`
1366 Reorder `extern crate` statements in group
1368 - **Default value**: `true`
1369 - **Possible values**: `true`, `false`
1371 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1373 #### `true` (default):
1397 See also [`reorder_extern_crates`](#reorder_extern_crates).
1401 Report `TODO` items in comments.
1403 - **Default value**: `"Never"`
1404 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1406 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1407 it contains a `#X` (with `X` being a number) in parentheses following the
1408 `TODO`, `"Unnumbered"` will ignore it.
1410 See also [`report_fixme`](#report_fixme).
1414 Report `FIXME` items in comments.
1416 - **Default value**: `"Never"`
1417 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1419 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1420 it contains a `#X` (with `X` being a number) in parentheses following the
1421 `FIXME`, `"Unnumbered"` will ignore it.
1423 See also [`report_todo`](#report_todo).
1428 Don't reformat out of line modules
1430 - **Default value**: `false`
1431 - **Possible values**: `true`, `false`
1433 ## `space_after_colon`
1435 Leave a space after the colon.
1437 - **Default value**: `true`
1438 - **Possible values**: `true`, `false`
1440 #### `true` (default):
1443 fn lorem<T: Eq>(t: T) {
1444 let lorem: Dolor = Lorem {
1454 fn lorem<T:Eq>(t:T) {
1455 let lorem:Dolor = Lorem {
1462 See also: [`space_before_colon`](#space_before_colon).
1464 ## `space_before_colon`
1466 Leave a space before the colon.
1468 - **Default value**: `false`
1469 - **Possible values**: `true`, `false`
1471 #### `false` (default):
1474 fn lorem<T: Eq>(t: T) {
1475 let lorem: Dolor = Lorem {
1485 fn lorem<T : Eq>(t : T) {
1486 let lorem : Dolor = Lorem {
1493 See also: [`space_after_colon`](#space_after_colon).
1495 ## `struct_field_align_threshold`
1497 The maximum diff of width between struct fields to be aligned with each other.
1499 - **Default value** : 0
1500 - **Possible values**: any positive integer
1524 ## `spaces_around_ranges`
1526 Put spaces around the .. and ... range operators
1528 - **Default value**: `false`
1529 - **Possible values**: `true`, `false`
1531 #### `false` (default):
1540 let lorem = 0 .. 10;
1543 ## `spaces_within_parens_and_brackets`
1545 Put spaces within non-empty generic arguments
1547 - **Default value**: `false`
1548 - **Possible values**: `true`, `false`
1550 #### `false` (default):
1553 fn lorem<T: Eq>(t: T) {
1561 fn lorem< T: Eq >(t: T) {
1566 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1568 ## `spaces_within_parens_and_brackets`
1570 Put spaces within non-empty parentheses
1572 - **Default value**: `false`
1573 - **Possible values**: `true`, `false`
1575 #### `false` (default):
1578 fn lorem<T: Eq>(t: T) {
1579 let lorem = (ipsum, dolor);
1586 fn lorem<T: Eq>( t: T ) {
1587 let lorem = ( ipsum, dolor );
1591 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1593 ## `spaces_within_parens_and_brackets`
1595 Put spaces within non-empty square brackets
1597 - **Default value**: `false`
1598 - **Possible values**: `true`, `false`
1600 #### `false` (default):
1603 let lorem: [usize; 2] = [ipsum, dolor];
1609 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1612 See also: [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets), [`spaces_within_parens_and_brackets`](#spaces_within_parens_and_brackets).
1614 ## `struct_lit_single_line`
1616 Put small struct literals on a single line
1618 - **Default value**: `true`
1619 - **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
1650 let ipsum = dolor();
1652 "amet consectetur adipiscing elit."
1661 let ipsum = dolor();
1663 "amet consectetur adipiscing elit."
1668 See also: [`hard_tabs`](#hard_tabs).
1673 How to handle trailing commas for lists
1675 - **Default value**: `"Vertical"`
1676 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1678 #### `"Vertical"` (default):
1681 let Lorem { ipsum, dolor, sit } = amet;
1695 let Lorem { ipsum, dolor, sit, } = amet;
1709 let Lorem { ipsum, dolor, sit } = amet;
1720 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1722 ## `trailing_semicolon`
1724 Add trailing semicolon after break, continue and return
1726 - **Default value**: `true`
1727 - **Possible values**: `true`, `false`
1729 #### `true` (default):
1743 ## `type_punctuation_density`
1745 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1747 - **Default value**: `"Wide"`
1748 - **Possible values**: `"Compressed"`, `"Wide"`
1750 #### `"Wide"` (default):
1753 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1758 #### `"Compressed"`:
1761 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1766 ## `use_try_shorthand`
1768 Replace uses of the try! macro by the ? shorthand
1770 - **Default value**: `false`
1771 - **Possible values**: `true`, `false`
1773 #### `false` (default):
1776 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1782 let lorem = ipsum.map(|dolor| dolor.sit())?;
1788 Break comments to fit on the line
1790 - **Default value**: `false`
1791 - **Possible values**: `true`, `false`
1793 #### `false` (default):
1796 // 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.
1802 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1803 // sed do eiusmod tempor incididunt ut labore et dolore
1804 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1805 // exercitation ullamco laboris nisi ut aliquip ex ea
1806 // commodo consequat.
1809 ## `wrap_match_arms`
1811 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1813 - **Default value**: `true`
1814 - **Possible values**: `true`, `false`
1816 #### `true` (default):
1821 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1823 false => println!("{}", sit),
1832 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1833 false => println!("{}", sit),
1837 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1841 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1843 - **Default value**: `"Overwrite"`
1844 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`