Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Oct 15, 2018
1 parent b737c34 commit 9bb238e
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 39 deletions.
31 changes: 17 additions & 14 deletions style-guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ communication overhead, and mental energy.

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

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

### Indentation and line width

Use spaces, not tabs. Each level of indentation must be four spaces. The maximum
width for a line is 100 characters. A tool should be configurable for all three
of these variables.
* Use spaces, not tabs.
* Each level of indentation must be four spaces (that is, all indentation
outside of string literals and comments must be a multiple of four).
* The maximum width for a line is 100 characters.
* A tool should be configurable for all three of these variables.


### Blank lines
Expand All @@ -37,7 +39,7 @@ two newlines). E.g,
```rust
fn foo() {
let x = ...;

let y = ...;
let z = ...;
}
Expand All @@ -60,10 +62,8 @@ defaults for both statements and items should be minimum: 1, maximum: 2.

### Comments

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

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

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

```rust
// A comment on an item.
Expand Down Expand Up @@ -129,9 +130,9 @@ Doc comments should come before attributes.

### Attributes

Put each attribute on its own line, indented to the indentation of its item.
In the case of inner attributes (`#!`), indent it to the inner indentation (the
indentation of the item + 1). Prefer outer attributes, where possible.
Put each attribute on its own line, indented to the level of the item.
In the case of inner attributes (`#!`), indent it to the level of the inside of
the item. Prefer outer attributes, where possible.

For attributes with argument lists, format like functions.

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

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

## [Cargo.toml conventions](cargo.md)

## [Principles used for deciding these guidelines](principles.md)
7 changes: 4 additions & 3 deletions style-guide/advice.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ if y {

## Names

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

### Modules

Expand Down
2 changes: 1 addition & 1 deletion style-guide/cargo.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ example, `MIT/Apache-2.0`.)
The homepage field, if present, must consist of a single URL, including the
scheme (e.g. `https://example.org/`, not just `example.org`.)

Within the description field, wrap text at 78 columns. Don't start the
Within the description field, wrap text at 80 columns. Don't start the
description field with the name of the crate (e.g. "cratename is a ..."); just
describe the crate itself. If providing a multi-sentence description, the first
sentence should go on a line by itself and summarize the crate, like the
Expand Down
49 changes: 38 additions & 11 deletions style-guide/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ An empty block should be written as `{}`.

A block may be written on a single line if:

* it is either
- used in expression position (not statement position)
- is an unsafe block in statement position
* it is either used in expression position (not statement position) or is an
unsafe block in statement position
* contains a single-line expression and no statements
* contains no comments

Expand Down Expand Up @@ -118,7 +117,7 @@ fn main() {
### Closures

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

Expand Down Expand Up @@ -185,7 +184,7 @@ let f = Foo {

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

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

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

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

```rust
Expand All @@ -429,7 +428,7 @@ let foo = bar
.qux();
```

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

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

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

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

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

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

Prefer


```rust
match foo {
foo => bar,
a_very_long_pattern
| another_pattern
| yet_another_pattern
| a_forth_pattern => {
...
}
}
```

Avoid splitting the left-hand side (before the `=>`) of a match arm where
possible. If the right-hand side of the match arm is kept on the same line,
never use a block (unless the block is empty).
Expand Down Expand Up @@ -724,6 +738,19 @@ clause, then you must use the above form:
}
```

If the pattern is multi-line, and the last line is less wide than the indent, do
not put the `if` clause on a newline. E.g.,

```rust
Token::Dimension {
value,
ref unit,
..
} if num_context.is_ok(context.parsing_mode, value) => {
...
}
```

If every clause in a pattern is *small*, but does not fit on one line, then the
pattern may be formatted across multiple lines with as many clauses per line as
possible. Again break before a `|`:
Expand Down
17 changes: 9 additions & 8 deletions style-guide/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ must come before other items. We recommend that imports come before module
declarations; if imports and modules are separated, then they should be ordered
alphabetically. When sorting, `self` and `super` must come before any other
names. Module declarations should not be moved if they are annotated with
`#[macro_export]`, since that may be semantics changing.
`#[macro_use]`, since that may be semantics changing.

Tools should make the above ordering optional.

Expand Down Expand Up @@ -77,9 +77,9 @@ enum FooBar {
}
```

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

```rust
enum FooBar {
Expand Down Expand Up @@ -118,7 +118,8 @@ struct Foo {
}
```

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

Expand Down Expand Up @@ -214,7 +215,7 @@ impl Bar for Foo {

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

```rust
impl Bar
Expand Down Expand Up @@ -425,7 +426,7 @@ associated type has a bound, there should be a space after the colon but not
before:

```rust
pub type Foo: Bar;
pub type Foo: Bar;
```


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

* `use a::self;` -> `use a;`
* `use a::{};` ->
* `use a::{};` -> (nothing)
* `use a::{b};` -> `use a::b;`

And must apply these recursively.
Expand All @@ -541,8 +542,8 @@ For example,
```rust
use a::b::{
x, y, z,
w::{...},
u::{...},
w::{...},
};
```

Expand Down
4 changes: 3 additions & 1 deletion style-guide/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let Foo {
Foo { f, g };

let (abcd,
defg):
defg):
Baz =
{ ... }
```
Expand Down Expand Up @@ -116,7 +116,9 @@ a_macro!(...);

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

```
<expr>;
```

All expressions in statement position should be terminated with a semi-colon,
unless they end with a block or are used as the value for a block.
Expand Down
2 changes: 1 addition & 1 deletion text/0000-style-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Rustfmt has many options for customising formatting. The behaviour of those opti
# Guide-level explanation
[guide-level-explanation]: #guide-level-explanation

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

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.

Expand Down

0 comments on commit 9bb238e

Please sign in to comment.