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):
48 let lorem = vec!["ipsum",
59 #### `"Block"` (default):
80 See also: [`control_brace_style`](#control_brace_style).
82 ### Function arguments
84 #### `"Block"` (default):
89 fn lorem(ipsum: usize) {}
109 fn lorem(ipsum: usize) {}
111 fn lorem(ipsum: usize,
124 #### `"Block"` (default):
154 #### `"Block"` (default):
162 Adipiscing: Eq = usize,
163 Consectetur: Eq = usize,
170 adipiscing: Adipiscing,
171 consectetur: Consectetur,
181 fn lorem<Ipsum: Eq = usize,
185 Adipiscing: Eq = usize,
186 Consectetur: Eq = usize,
192 adipiscing: Adipiscing,
193 consectetur: Consectetur,
202 #### `"Block"` (default):
214 let lorem = Lorem { ipsum: dolor,
218 See also: [`struct_lit_single_line`](#struct_lit_single_line), [`indent_style`](#indent_style).
222 #### `"Block"` (default):
225 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
239 fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
250 ## `same_line_attributes`
252 Try to put attributes on the same line as fields and variants
254 - **Default value**: `true`
255 - **Possible values**: `true`, `false`
258 #### `true` (default):
262 #[serde(rename = "Ipsum")] ipsum: usize,
263 #[serde(rename = "Dolor")] dolor: usize,
264 #[serde(rename = "Amet")] amet: usize,
268 #[serde(skip_serializing)] Ipsum,
269 #[serde(skip_serializing)] Dolor,
270 #[serde(skip_serializing)] Amet,
278 #[serde(rename = "Ipsum")]
280 #[serde(rename = "Dolor")]
282 #[serde(rename = "Amet")]
287 #[serde(skip_serializing)]
289 #[serde(skip_serializing)]
291 #[serde(skip_serializing)]
296 ## `use_small_heuristics`
298 Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
300 - **Default value**: `true`
301 - **Possible values**: `true`, `false`
304 #### `true` (default):
310 Sit { amet: Consectetur, adipiscing: Elit },
324 let lorem = Lorem { ipsum: dolor, sit: amet };
326 let lorem = if ipsum { dolor } else { sit };
343 lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
350 let lorem = if ipsum {
360 Where to put a binary operator when a binary expression goes multiline.
362 - **Default value**: `"Front"`
363 - **Possible values**: `"Front"`, `"Back"`
366 #### `"Front"` (default):
377 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
378 ..bbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
392 let range = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..
393 bbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
396 ## `combine_control_expr`
398 Combine control expressions with function calls.
400 - **Default value**: `true`
401 - **Possible values**: `true`, `false`
404 #### `true` (default):
416 foo!(if let Some(..) = x {
429 foo!(while let Some(..) = x {
463 if let Some(..) = x {
480 while let Some(..) = x {
506 Maximum length of comments. No effect unless`wrap_comments = true`.
508 - **Default value**: `80`
509 - **Possible values**: any positive integer
512 **Note:** A value of `0` results in [`wrap_comments`](#wrap_comments) being applied regardless of a line's width.
514 #### `80` (default; comments shorter than `comment_width`):
516 // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
519 #### `60` (comments longer than `comment_width`):
521 // Lorem ipsum dolor sit amet,
522 // consectetur adipiscing elit.
525 See also [`wrap_comments`](#wrap_comments).
527 ## `condense_wildcard_suffixes`
529 Replace strings of _ wildcards by a single .. in tuple patterns
531 - **Default value**: `false`
532 - **Possible values**: `true`, `false`
535 #### `false` (default):
538 let (lorem, ipsum, _, _) = (1, 2, 3, 4);
544 let (lorem, ipsum, ..) = (1, 2, 3, 4);
547 ## `control_brace_style`
549 Brace style for control flow constructs
551 - **Default value**: `"AlwaysSameLine"`
552 - **Possible values**: `"AlwaysNextLine"`, `"AlwaysSameLine"`, `"ClosingNextLine"`
555 #### `"AlwaysSameLine"` (default):
565 #### `"AlwaysNextLine"`:
578 #### `"ClosingNextLine"`:
589 ## `disable_all_formatting`
591 Don't reformat anything
593 - **Default value**: `false`
594 - **Possible values**: `true`, `false`
597 ## `error_on_line_overflow`
599 Error if unable to get all lines within `max_width`
601 - **Default value**: `true`
602 - **Possible values**: `true`, `false`
605 See also [`max_width`](#max_width).
607 ## `error_on_line_overflow_comments`
609 Error if unable to get all comment lines within `comment_width`.
611 - **Default value**: `true`
612 - **Possible values**: `true`, `false`
615 See also [`comment_width`](#comment_width).
619 Argument density in functions
621 - **Default value**: `"Tall"`
622 - **Possible values**: `"Compressed"`, `"Tall"`, `"Vertical"`
625 #### `"Tall"` (default):
629 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
631 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
640 consectetur: Consectetur,
641 adipiscing: Adipiscing,
650 consectetur: Consectetur,
651 adipiscing: Adipiscing,
663 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet);
665 fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
670 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
671 adipiscing: Adipiscing, elit: Elit,
675 ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: Consectetur,
676 adipiscing: Adipiscing, elit: Elit,
687 fn lorem(ipsum: Ipsum,
692 fn lorem(ipsum: Ipsum,
699 fn lorem(ipsum: Ipsum,
703 consectetur: Consectetur,
704 adipiscing: Adipiscing,
707 fn lorem(ipsum: Ipsum,
711 consectetur: Consectetur,
712 adipiscing: Adipiscing,
722 Brace style for items
724 - **Default value**: `"SameLineWhere"`
725 - **Possible values**: `"AlwaysNextLine"`, `"PreferSameLine"`, `"SameLineWhere"`
730 #### `"SameLineWhere"` (default):
737 fn lorem(ipsum: usize) {
741 fn lorem<T>(ipsum: T)
743 T: Add + Sub + Mul + Div,
749 #### `"AlwaysNextLine"`:
757 fn lorem(ipsum: usize)
762 fn lorem<T>(ipsum: T)
764 T: Add + Sub + Mul + Div,
770 #### `"PreferSameLine"`:
777 fn lorem(ipsum: usize) {
781 fn lorem<T>(ipsum: T)
783 T: Add + Sub + Mul + Div, {
788 ### Structs and enums
790 #### `"SameLineWhere"` (default):
804 #### `"AlwaysNextLine"`:
819 #### `"PreferSameLine"`:
833 ## `empty_item_single_line`
835 Put empty-body functions and impls on a single line
837 - **Default value**: `true`
838 - **Possible values**: `true`, `false`
841 #### `true` (default):
859 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
864 Put single-expression functions on a single line
866 - **Default value**: `false`
867 - **Possible values**: `true`, `false`
870 #### `false` (default):
873 fn lorem() -> usize {
877 fn lorem() -> usize {
886 fn lorem() -> usize { 42 }
888 fn lorem() -> usize {
894 See also [`control_brace_style`](#control_brace_style).
897 ## `where_single_line`
899 To force single line where layout
901 - **Default value**: `false`
902 - **Possible values**: `true`, `false`
905 #### `false` (default):
920 where Option<T>: Ipsum {
925 See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_style).
928 ## `force_explicit_abi`
930 Always print the abi for extern items
932 - **Default value**: `true`
933 - **Possible values**: `true`, `false`
936 **Note:** Non-"C" ABIs are always printed. If `false` then "C" is removed.
938 #### `true` (default):
942 pub static lorem: c_int;
950 pub static lorem: c_int;
956 Format string literals where necessary
958 - **Default value**: `false`
959 - **Possible values**: `true`, `false`
962 #### `false` (default):
965 let lorem = "ipsum dolor sit amet consectetur adipiscing elit lorem ipsum dolor sit";
972 "ipsum dolor sit amet consectetur \
973 adipiscing elit lorem ipsum dolor sit";
976 See also [`max_width`](#max_width).
980 Use tab characters for indentation, spaces for alignment
982 - **Default value**: `false`
983 - **Possible values**: `true`, `false`
986 #### `false` (default):
989 fn lorem() -> usize {
990 42 // spaces before 42
997 fn lorem() -> usize {
1002 See also: [`tab_spaces`](#tab_spaces).
1007 Indent style of imports
1009 - **Default Value**: `"Visual"`
1010 - **Possible values**: `"Block"`, `"Visual"`
1013 #### `"Visual"` (default):
1031 See also: [`imports_layout`](#imports_layout).
1035 Item layout inside a imports block
1037 - **Default value**: "Mixed"
1038 - **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1041 #### `"Mixed"` (default):
1044 use foo::{xxx, yyy, zzz};
1046 use foo::{aaa, bbb, ccc,
1050 #### `"Horizontal"`:
1052 **Note**: This option forces to put everything on one line and may exceeds `max_width`.
1055 use foo::{xxx, yyy, zzz};
1057 use foo::{aaa, bbb, ccc, ddd, eee, fff};
1060 #### `"HorizontalVertical"`:
1063 use foo::{xxx, yyy, zzz};
1089 ## `match_block_trailing_comma`
1091 Put a trailing comma after a block based match arm (non-block arms are not affected)
1093 - **Default value**: `false`
1094 - **Possible values**: `true`, `false`
1097 #### `false` (default):
1104 Lorem::Dolor => println!("dolor"),
1115 Lorem::Dolor => println!("dolor"),
1119 See also: [`trailing_comma`](#trailing_comma), [`match_arm_blocks`](#match_arm_blocks).
1123 Maximum width of each line
1125 - **Default value**: `100`
1126 - **Possible values**: any positive integer
1129 See also [`error_on_line_overflow`](#error_on_line_overflow).
1133 Merge multiple derives into a single one.
1135 - **Default value**: `true`
1136 - **Possible values**: `true`, `false`
1139 #### `true` (default):
1142 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
1149 #[derive(Eq, PartialEq)]
1151 #[derive(Copy, Clone)]
1155 ## `force_multiline_blocks`
1157 Force multiline closure and match arm bodies to be wrapped in a block
1159 - **Default value**: `false`
1160 - **Possible values**: `false`, `true`
1163 #### `false` (default):
1166 result.and_then(|maybe_value| match maybe_value {
1173 println!("Hello World");
1183 result.and_then(|maybe_value| {
1193 println!("Hello World");
1203 Unix or Windows line endings
1205 - **Default value**: `"Unix"`
1206 - **Possible values**: `"Native"`, `"Unix"`, `"Windows"`
1209 ## `normalize_comments`
1211 Convert /* */ comments to // comments where possible
1213 - **Default value**: `false`
1214 - **Possible values**: `true`, `false`
1217 #### `false` (default):
1221 fn dolor() -> usize {}
1224 fn adipiscing() -> usize {}
1231 fn dolor() -> usize {}
1234 fn adipiscing() -> usize {}
1237 ## `reorder_imported_names`
1239 Reorder lists of names in import statements alphabetically
1241 - **Default value**: `false`
1242 - **Possible values**: `true`, `false`
1245 #### `false` (default):
1248 use super::{lorem, ipsum, dolor, sit};
1254 use super::{dolor, ipsum, lorem, sit};
1257 See also [`reorder_imports`](#reorder_imports).
1259 ## `reorder_imports`
1261 Reorder import statements alphabetically
1263 - **Default value**: `false`
1264 - **Possible values**: `true`, `false`
1267 #### `false` (default):
1285 See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group).
1287 ## `reorder_imports_in_group`
1289 Reorder import statements in group
1291 - **Default value**: `false`
1292 - **Possible values**: `true`, `false`
1295 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1297 #### `false` (default):
1321 See also [`reorder_imports`](#reorder_imports).
1323 ## `reorder_extern_crates`
1325 Reorder `extern crate` statements alphabetically
1327 - **Default value**: `true`
1328 - **Possible values**: `true`, `false`
1331 #### `true` (default):
1349 See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group).
1351 ## `reorder_extern_crates_in_group`
1353 Reorder `extern crate` statements in group
1355 - **Default value**: `true`
1356 - **Possible values**: `true`, `false`
1359 **Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
1361 #### `true` (default):
1385 See also [`reorder_extern_crates`](#reorder_extern_crates).
1389 Report `TODO` items in comments.
1391 - **Default value**: `"Never"`
1392 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1395 Warns about any comments containing `TODO` in them when set to `"Always"`. If
1396 it contains a `#X` (with `X` being a number) in parentheses following the
1397 `TODO`, `"Unnumbered"` will ignore it.
1399 See also [`report_fixme`](#report_fixme).
1403 Report `FIXME` items in comments.
1405 - **Default value**: `"Never"`
1406 - **Possible values**: `"Always"`, `"Unnumbered"`, `"Never"`
1409 Warns about any comments containing `FIXME` in them when set to `"Always"`. If
1410 it contains a `#X` (with `X` being a number) in parentheses following the
1411 `FIXME`, `"Unnumbered"` will ignore it.
1413 See also [`report_todo`](#report_todo).
1418 Don't reformat out of line modules
1420 - **Default value**: `false`
1421 - **Possible values**: `true`, `false`
1424 ## `space_after_colon`
1426 Leave a space after the colon.
1428 - **Default value**: `true`
1429 - **Possible values**: `true`, `false`
1432 #### `true` (default):
1435 fn lorem<T: Eq>(t: T) {
1436 let lorem: Dolor = Lorem {
1446 fn lorem<T:Eq>(t:T) {
1447 let lorem:Dolor = Lorem {
1454 See also: [`space_before_colon`](#space_before_colon).
1456 ## `space_before_colon`
1458 Leave a space before the colon.
1460 - **Default value**: `false`
1461 - **Possible values**: `true`, `false`
1464 #### `false` (default):
1467 fn lorem<T: Eq>(t: T) {
1468 let lorem: Dolor = Lorem {
1478 fn lorem<T : Eq>(t : T) {
1479 let lorem : Dolor = Lorem {
1486 See also: [`space_after_colon`](#space_after_colon).
1488 ## `struct_field_align_threshold`
1490 The maximum diff of width between struct fields to be aligned with each other.
1492 - **Default value** : 0
1493 - **Possible values**: any positive integer
1516 ## `spaces_around_ranges`
1518 Put spaces around the .. and ... range operators
1520 - **Default value**: `false`
1521 - **Possible values**: `true`, `false`
1524 #### `false` (default):
1533 let lorem = 0 .. 10;
1536 ## `spaces_within_parens_and_brackets`
1538 Put spaces within non-empty generic arguments, parentheses, and square brackets
1540 - **Default value**: `false`
1541 - **Possible values**: `true`, `false`
1544 #### `false` (default):
1547 // generic arguments
1548 fn lorem<T: Eq>(t: T) {
1552 // non-empty parentheses
1553 fn lorem<T: Eq>(t: T) {
1554 let lorem = (ipsum, dolor);
1557 // non-empty square brackets
1558 let lorem: [usize; 2] = [ipsum, dolor];
1564 // generic arguments
1565 fn lorem< T: Eq >(t: T) {
1569 // non-empty parentheses
1570 fn lorem<T: Eq>( t: T ) {
1571 let lorem = ( ipsum, dolor );
1574 // non-empty square brackets
1575 let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1578 ## `struct_lit_single_line`
1580 Put small struct literals on a single line
1582 - **Default value**: `true`
1583 - **Possible values**: `true`, `false`
1586 #### `true` (default):
1589 let lorem = Lorem { ipsum: dolor, sit: amet };
1601 See also: [`indent_style`](#indent_style).
1606 Number of spaces per tab
1608 - **Default value**: `4`
1609 - **Possible values**: any positive integer
1616 let ipsum = dolor();
1618 "amet consectetur adipiscing elit."
1627 let ipsum = dolor();
1629 "amet consectetur adipiscing elit."
1634 See also: [`hard_tabs`](#hard_tabs).
1639 How to handle trailing commas for lists
1641 - **Default value**: `"Vertical"`
1642 - **Possible values**: `"Always"`, `"Never"`, `"Vertical"`
1645 #### `"Vertical"` (default):
1648 let Lorem { ipsum, dolor, sit } = amet;
1662 let Lorem { ipsum, dolor, sit, } = amet;
1676 let Lorem { ipsum, dolor, sit } = amet;
1687 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1689 ## `trailing_semicolon`
1691 Add trailing semicolon after break, continue and return
1693 - **Default value**: `true`
1694 - **Possible values**: `true`, `false`
1697 #### `true` (default):
1711 ## `type_punctuation_density`
1713 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
1715 - **Default value**: `"Wide"`
1716 - **Possible values**: `"Compressed"`, `"Wide"`
1719 #### `"Wide"` (default):
1722 fn lorem<Ipsum: Dolor + Sit = Amet>() {
1727 #### `"Compressed"`:
1730 fn lorem<Ipsum: Dolor+Sit=Amet>() {
1735 ## `use_try_shorthand`
1737 Replace uses of the try! macro by the ? shorthand
1739 - **Default value**: `false`
1740 - **Possible values**: `true`, `false`
1743 #### `false` (default):
1746 let lorem = try!(ipsum.map(|dolor|dolor.sit()));
1752 let lorem = ipsum.map(|dolor| dolor.sit())?;
1758 Break comments to fit on the line
1760 - **Default value**: `false`
1761 - **Possible values**: `true`, `false`
1764 #### `false` (default):
1767 // 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.
1773 // Lorem ipsum dolor sit amet, consectetur adipiscing elit,
1774 // sed do eiusmod tempor incididunt ut labore et dolore
1775 // magna aliqua. Ut enim ad minim veniam, quis nostrud
1776 // exercitation ullamco laboris nisi ut aliquip ex ea
1777 // commodo consequat.
1780 ## `match_arm_blocks`
1782 Wrap the body of arms in blocks when it does not fit on the same line with the pattern of arms
1784 - **Default value**: `true`
1785 - **Possible values**: `true`, `false`
1788 #### `true` (default):
1793 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
1795 false => println!("{}", sit),
1804 foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
1805 false => println!("{}", sit),
1809 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
1813 What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage
1815 - **Default value**: `"Overwrite"`
1816 - **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`
1819 ## `blank_lines_upper_bound`
1821 Maximum number of blank lines which can be put between items. If more than this number of consecutive empty
1822 lines are found, they are trimmed down to match this integer.
1824 - **Default value**: `1`
1825 - **Possible values**: *unsigned integer*
1874 See also: [`blank_lines_lower_bound`](#blank_lines_lower_bound)
1876 ## `blank_lines_lower_bound`
1878 Minimum number of blank lines which must be put between items. If two items have fewer blank lines between
1879 them, additional blank lines are inserted.
1881 - **Default value**: `0`
1882 - **Possible values**: *unsigned integer*
1886 Original Code (rustfmt will not change it with the default value of `0`):