Skip to content

Commit 9bb238e

Browse files
committed
Address feedback
1 parent b737c34 commit 9bb238e

File tree

7 files changed

+73
-39
lines changed

7 files changed

+73
-39
lines changed

style-guide/README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ communication overhead, and mental energy.
1313

1414
Humans comprehend information through pattern matching. By ensuring that all
1515
Rust code has similar formatting, less mental effort is required to comprehend a
16-
new project, lowering the bar to entry for new developers.
16+
new project, lowering the barrier to entry for new developers.
1717

1818
Thus, there are productivity benefits to using a formatting tool (such as
1919
rustfmt), and even larger benefits by using a community-consistent formatting,
@@ -24,9 +24,11 @@ typically by using a formatting tool's default settings.
2424

2525
### Indentation and line width
2626

27-
Use spaces, not tabs. Each level of indentation must be four spaces. The maximum
28-
width for a line is 100 characters. A tool should be configurable for all three
29-
of these variables.
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.
3032

3133

3234
### Blank lines
@@ -37,7 +39,7 @@ two newlines). E.g,
3739
```rust
3840
fn foo() {
3941
let x = ...;
40-
42+
4143
let y = ...;
4244
let z = ...;
4345
}
@@ -60,10 +62,8 @@ defaults for both statements and items should be minimum: 1, maximum: 2.
6062

6163
### Comments
6264

63-
The following guidelines are recommendations only, a mechanical formatter should
64-
not change comments except to move them within a file. To be clear this means
65-
changing the whitespace before a line comment or the whitespace before or after
66-
a block comment.
65+
The following guidelines for comments are recommendations only, a mechanical
66+
formatter might skip formatting of comments.
6767

6868
Prefer line comments (`//`) to block comments (`/* ... */`).
6969

@@ -76,7 +76,8 @@ have a newline after the opening sigil and before the closing sigil.
7676
Prefer to put a comment on its own line. Where a comment follows code, there
7777
should be a single space before it. Where a block comment is inline, there
7878
should be surrounding whitespace as if it were an identifier or keyword. There
79-
should be no trailing whitespace after a comment. Examples:
79+
should be no trailing whitespace after a comment or at the end of any line in a
80+
multi-line comment. Examples:
8081

8182
```rust
8283
// A comment on an item.
@@ -129,9 +130,9 @@ Doc comments should come before attributes.
129130

130131
### Attributes
131132

132-
Put each attribute on its own line, indented to the indentation of its item.
133-
In the case of inner attributes (`#!`), indent it to the inner indentation (the
134-
indentation of the item + 1). Prefer outer attributes, where possible.
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.
135136

136137
For attributes with argument lists, format like functions.
137138

@@ -176,7 +177,7 @@ circumstances.
176177
Some suitable heuristics are the size of the item (in characters) or the
177178
complexity of an item (for example, that all components must be simple names,
178179
not more complex sub-expressions). For more discussion on suitable heuristics,
179-
see the discussion on [this issue](https://github.com/rust-lang-nursery/fmt-rfcs/issues/47).
180+
see [this issue](https://github.com/rust-lang-nursery/fmt-rfcs/issues/47).
180181

181182
Tools should give the user an option to ignore such heuristics and always use
182183
the normal formatting.
@@ -185,3 +186,5 @@ the normal formatting.
185186
## [Non-formatting conventions](advice.md)
186187

187188
## [Cargo.toml conventions](cargo.md)
189+
190+
## [Principles used for deciding these guidelines](principles.md)

style-guide/advice.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ if y {
1818

1919
## Names
2020

21-
* Types shall be `PascalCase`,
22-
* Enum variants shall be `PascalCase`,
21+
* Types shall be `UpperCamelCase`,
22+
* Enum variants shall be `UpperCamelCase`,
2323
* Struct fields shall be `snake_case`,
2424
* Function and method names shall be `snake_case`,
2525
* Local variables shall be `snake_case`,
2626
* Macro names shall be `snake_case`,
2727
* Constants (`const`s and immutable `static`s) shall be `SCREAMING_SNAKE_CASE`.
2828
* When a name is forbidden because it is a reserved word (e.g., `crate`), use a
29-
trailing underscore to make the name legal (e.g., `crate_`).
29+
trailing underscore to make the name legal (e.g., `crate_`), or use raw
30+
identifiers if possible.
3031

3132
### Modules
3233

style-guide/cargo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ example, `MIT/Apache-2.0`.)
7070
The homepage field, if present, must consist of a single URL, including the
7171
scheme (e.g. `https://example.org/`, not just `example.org`.)
7272

73-
Within the description field, wrap text at 78 columns. Don't start the
73+
Within the description field, wrap text at 80 columns. Don't start the
7474
description field with the name of the crate (e.g. "cratename is a ..."); just
7575
describe the crate itself. If providing a multi-sentence description, the first
7676
sentence should go on a line by itself and summarize the crate, like the

style-guide/expressions.md

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ An empty block should be written as `{}`.
6060

6161
A block may be written on a single line if:
6262

63-
* it is either
64-
- used in expression position (not statement position)
65-
- is an unsafe block in statement position
63+
* it is either used in expression position (not statement position) or is an
64+
unsafe block in statement position
6665
* contains a single-line expression and no statements
6766
* contains no comments
6867

@@ -118,7 +117,7 @@ fn main() {
118117
### Closures
119118

120119
Don't put any extra spaces before the first `|` (unless the closure is prefixed
121-
by `move`), but put a space between the second `|` and the expression of the
120+
by `move`); put a space between the second `|` and the expression of the
122121
closure. Between the `|`s, you should use function definition syntax, however,
123122
elide types where possible.
124123

@@ -185,7 +184,7 @@ let f = Foo {
185184

186185
Use a single-line form where possible. There should not be spaces around the
187186
parentheses. Where a single-line form is not possible, each element of the tuple
188-
should be on it's own block-indented line and there should be a trailing comma.
187+
should be on its own block-indented line and there should be a trailing comma.
189188

190189
```rust
191190
(a, b, c)
@@ -416,11 +415,11 @@ let cstr = "Hi\0" as *const str as *const [u8] as *const std::os::raw::c_char;
416415
### Chains of fields and method calls
417416

418417
A chain is a sequence of field accesses and/or method calls. A chain may also
419-
include the try operator. E.g., `a.b.c().d` or `foo?.bar().baz?`.
418+
include the try operator ('?'). E.g., `a.b.c().d` or `foo?.bar().baz?`.
420419

421420
Prefer formatting on one line if possible, and the chain is *small*. If
422421
formatting on multiple lines, each field access or method call in the chain
423-
should be on it's own line with the line-break before the `.` and after any `?`.
422+
should be on its own line with the line-break before the `.` and after any `?`.
424423
Each line should be block-indented. E.g.,
425424

426425
```rust
@@ -429,7 +428,7 @@ let foo = bar
429428
.qux();
430429
```
431430

432-
If the length of the last line of the first element plus it's indentation is
431+
If the length of the last line of the first element plus its indentation is
433432
less than or equal to the indentation of the second line (and there is space),
434433
then combine the first and second lines, e.g.,
435434

@@ -467,7 +466,7 @@ a.b.c()?.d
467466
Note there is block indent due to the chain and the function call in the above
468467
example.
469468

470-
Prefer formatting the whole chain in mulit-line style and each element on one
469+
Prefer formatting the whole chain in multi-line style and each element on one
471470
line, rather than putting some elements on multiple lines and some on a single
472471
line, e.g.,
473472

@@ -490,8 +489,8 @@ This section covers `if`, `if let`, `loop`, `while`, `while let`, and `for`
490489
expressions.
491490

492491
The keyword, any initial clauses, and the opening brace of the block should be
493-
on a single line. The usual rules for block formatting should be applied to the
494-
block.
492+
on a single line. The usual rules for [block formatting](#Blocks) should be
493+
applied to the block.
495494

496495
If there is an `else` component, then the closing brace, `else`, any following
497496
clause, and the opening brace should all be on the same line. There should be a
@@ -614,6 +613,21 @@ match foo {
614613
}
615614
```
616615

616+
Prefer
617+
618+
619+
```rust
620+
match foo {
621+
foo => bar,
622+
a_very_long_pattern
623+
| another_pattern
624+
| yet_another_pattern
625+
| a_forth_pattern => {
626+
...
627+
}
628+
}
629+
```
630+
617631
Avoid splitting the left-hand side (before the `=>`) of a match arm where
618632
possible. If the right-hand side of the match arm is kept on the same line,
619633
never use a block (unless the block is empty).
@@ -724,6 +738,19 @@ clause, then you must use the above form:
724738
}
725739
```
726740

741+
If the pattern is multi-line, and the last line is less wide than the indent, do
742+
not put the `if` clause on a newline. E.g.,
743+
744+
```rust
745+
Token::Dimension {
746+
value,
747+
ref unit,
748+
..
749+
} if num_context.is_ok(context.parsing_mode, value) => {
750+
...
751+
}
752+
```
753+
727754
If every clause in a pattern is *small*, but does not fit on one line, then the
728755
pattern may be formatted across multiple lines with as many clauses per line as
729756
possible. Again break before a `|`:

style-guide/items.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ must come before other items. We recommend that imports come before module
88
declarations; if imports and modules are separated, then they should be ordered
99
alphabetically. When sorting, `self` and `super` must come before any other
1010
names. Module declarations should not be moved if they are annotated with
11-
`#[macro_export]`, since that may be semantics changing.
11+
`#[macro_use]`, since that may be semantics changing.
1212

1313
Tools should make the above ordering optional.
1414

@@ -77,9 +77,9 @@ enum FooBar {
7777
}
7878
```
7979

80-
If a struct variant is *short* (TODO link to definition), it may be formatted on
80+
If a struct variant is [*small*](#small-items), it may be formatted on
8181
one line. In this case, do not use a trailing comma for the field list, but do
82-
put spaces around braces:
82+
put spaces around each brace:
8383

8484
```rust
8585
enum FooBar {
@@ -118,7 +118,8 @@ struct Foo {
118118
}
119119
```
120120

121-
Prefer using a unit struct to an empty struct (these only exist to simplify code
121+
Prefer using a unit struct (e.g., `struct Foo;`) to an empty struct (e.g.,
122+
`struct Foo();` or `struct Foo {}`, these only exist to simplify code
122123
generation), but if you must use an empty struct, keep it on one line with no
123124
space between the braces: `struct Foo;` or `struct Foo {}`.
124125

@@ -214,7 +215,7 @@ impl Bar for Foo {
214215

215216
Avoid line-breaking in the signature if possible. If a line break is required in
216217
a non-inherent impl, break immediately before `for`, block indent the concrete type
217-
and put the opening brace on it's own line:
218+
and put the opening brace on its own line:
218219

219220
```rust
220221
impl Bar
@@ -425,7 +426,7 @@ associated type has a bound, there should be a space after the colon but not
425426
before:
426427

427428
```rust
428-
pub type Foo: Bar;
429+
pub type Foo: Bar;
429430
```
430431

431432

@@ -521,7 +522,7 @@ example, `a::*` comes before `b::a` but `a::b` comes before `a::*`. E.g.,
521522
Tools must make the following normalisations:
522523

523524
* `use a::self;` -> `use a;`
524-
* `use a::{};` ->
525+
* `use a::{};` -> (nothing)
525526
* `use a::{b};` -> `use a::b;`
526527

527528
And must apply these recursively.
@@ -541,8 +542,8 @@ For example,
541542
```rust
542543
use a::b::{
543544
x, y, z,
544-
w::{...},
545545
u::{...},
546+
w::{...},
546547
};
547548
```
548549

style-guide/statements.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let Foo {
4343
Foo { f, g };
4444

4545
let (abcd,
46-
defg):
46+
defg):
4747
Baz =
4848
{ ... }
4949
```
@@ -116,7 +116,9 @@ a_macro!(...);
116116

117117
There should be no space between the expression and the semi-colon.
118118

119+
```
119120
<expr>;
121+
```
120122

121123
All expressions in statement position should be terminated with a semi-colon,
122124
unless they end with a block or are used as the value for a block.

text/0000-style-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Rustfmt has many options for customising formatting. The behaviour of those opti
3030
# Guide-level explanation
3131
[guide-level-explanation]: #guide-level-explanation
3232

33-
See the [guide text](../guide/README.md).
33+
See the [guide text](../style-guide/README.md).
3434

3535
The style guide lives in the RFC repo, since it can be considered an appendix to this RFC. We might want to duplicate the style guide elsewhere to make it more discoverable, but the version here will be the 'source of truth'. Amendments to the style guide should go through the RFC process.
3636

0 commit comments

Comments
 (0)