3 ## Motivation - why use a formatting tool?
5 Formatting code is a mostly mechanical task which takes both time and mental
6 effort. By using an automatic formatting tool, a programmer is relieved of
7 this task and can concentrate on more important things.
9 Furthermore, by sticking to an established style guide (such as this one),
10 programmers don't need to formulate ad hoc style rules, nor do they need to
11 debate with other programmers what style rules should be used, saving time,
12 communication overhead, and mental energy.
14 Humans comprehend information through pattern matching. By ensuring that all
15 Rust code has similar formatting, less mental effort is required to comprehend a
16 new project, lowering the barrier to entry for new developers.
18 Thus, there are productivity benefits to using a formatting tool (such as
19 rustfmt), and even larger benefits by using a community-consistent formatting,
20 typically by using a formatting tool's default settings.
23 ## Formatting conventions
25 ### Indentation and line width
27 * Use spaces, not tabs.
28 * Each level of indentation must be four spaces (that is, all indentation
29 outside of string literals and comments must be a multiple of four).
30 * The maximum width for a line is 100 characters.
31 * A tool should be configurable for all three of these variables.
36 Separate items and statements by either zero or one blank lines (i.e., one or
51 Formatting tools should make the bounds on blank lines configurable: there
52 should be separate minimum and maximum numbers of newlines between both
53 statements and (top-level) items (i.e., four options). As described above, the
54 defaults for both statements and items should be minimum: 1, maximum: 2.
57 ### [Module-level items](items.md)
58 ### [Statements](statements.md)
59 ### [Expressions](expressions.md)
65 The following guidelines for comments are recommendations only, a mechanical
66 formatter might skip formatting of comments.
68 Prefer line comments (`//`) to block comments (`/* ... */`).
70 When using line comments there should be a single space after the opening sigil.
72 When using single-line block comments there should be a single space after the
73 opening sigil and before the closing sigil. Multi-line block comments should
74 have a newline after the opening sigil and before the closing sigil.
76 Prefer to put a comment on its own line. Where a comment follows code, there
77 should be a single space before it. Where a block comment is inline, there
78 should be surrounding whitespace as if it were an identifier or keyword. There
79 should be no trailing whitespace after a comment or at the end of any line in a
80 multi-line comment. Examples:
83 // A comment on an item.
86 fn foo() {} // A comment after an item.
88 pub fn foo(/* a comment before an argument */ x: T) {...}
91 Comments should usually be complete sentences. Start with a capital letter, end
92 with a period (`.`). An inline block comment may be treated as a note without
95 Source lines which are entirely a comment should be limited to 80 characters
96 in length (including comment sigils, but excluding indentation) or the maximum
97 width of the line (including comment sigils and indentation), whichever is
101 // This comment goes up to the ................................. 80 char margin.
104 // This comment is .............................................. 80 chars wide.
113 // This comment is limited by the ......................... 100 char margin.
124 Prefer line comments (`///`) to block comments (`/** ... */`).
126 Prefer outer doc comments (`///` or `/** ... */`), only use inner doc comments
127 (`//!` and `/*! ... */`) to write module-level or crate-level documentation.
129 Doc comments should come before attributes.
133 Put each attribute on its own line, indented to the level of the item.
134 In the case of inner attributes (`#!`), indent it to the level of the inside of
135 the item. Prefer outer attributes, where possible.
137 For attributes with argument lists, format like functions.
149 For attributes with an equal sign, there should be a single space before and
150 after the `=`, e.g., `#[foo = 42]`.
152 There must only be a single `derive` attribute. Note for tool authors: if
153 combining multiple `derive` attributes into a single attribute, the ordering of
154 the derived names should be preserved. E.g., `#[derive(bar)] #[derive(foo)]
155 struct Baz;` should be formatted to `#[derive(bar, foo)] struct Baz;`.
159 In many places in this guide we specify that a formatter may format an item
160 differently if it is *small*, for example struct literals:
166 f2: another_expression(),
169 // *small* formatting
173 We leave it to individual tools to decide on exactly what *small* means. In
174 particular, tools are free to use different definitions in different
177 Some suitable heuristics are the size of the item (in characters) or the
178 complexity of an item (for example, that all components must be simple names,
179 not more complex sub-expressions). For more discussion on suitable heuristics,
180 see [this issue](https://github.com/rust-lang-nursery/fmt-rfcs/issues/47).
182 Tools should give the user an option to ignore such heuristics and always use
183 the normal formatting.
186 ## [Non-formatting conventions](advice.md)
188 ## [Cargo.toml conventions](cargo.md)
190 ## [Principles used for deciding these guidelines](principles.md)