mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
Rollup merge of #112942 - joshtriplett:style-guide-tweaks, r=compiler-errors
style-guide: Organizational and editing tweaks (no semantic changes) I'd recommend reviewing this PR commit-by-commit; each commit is self-contained and should be easy to review at a glance. - style-guide: Move text about block vs visual indent to indentation section - style-guide: Move and expand text about trailing commas - style-guide: s/right-ward/rightward/ - style-guide: Consistently refer to rustfmt as `rustfmt` - style-guide: Remove inaccurate statement about rustfmt - style-guide: Define (and capitalize) "ASCIIbetically" - style-guide: Update cargo.md for authors being optional and not recommended - style-guide: Avoid normative recommendations for formatting tool configurability - style-guide: Clarify advice on names matching keywords - style-guide: Reword an awkwardly phrased recommendation (and fix a typo) - style-guide: Rephrase a confusingly ordered, ambiguous sentence (and fix a typo) - style-guide: Avoid hyphenating "semicolon" - style-guide: Make link text in SUMMARY.md match the headings in the linked pages - style-guide: Define what an item is - style-guide: Avoid referring to the style team in the past tense
This commit is contained in:
commit
441e59ad6c
@ -16,8 +16,8 @@ Rust code has similar formatting, less mental effort is required to comprehend a
|
||||
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,
|
||||
typically by using a formatting tool's default settings.
|
||||
`rustfmt`), and even larger benefits by using a community-consistent
|
||||
formatting, typically by using a formatting tool's default settings.
|
||||
|
||||
|
||||
## Formatting conventions
|
||||
@ -28,8 +28,47 @@ typically by using a formatting tool's default settings.
|
||||
* 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.
|
||||
* A tool may choose to make some of these configurable.
|
||||
|
||||
#### Block indent
|
||||
|
||||
Prefer block indent over visual indent:
|
||||
|
||||
```rust
|
||||
// Block indent
|
||||
a_function_call(
|
||||
foo,
|
||||
bar,
|
||||
);
|
||||
|
||||
// Visual indent
|
||||
a_function_call(foo,
|
||||
bar);
|
||||
```
|
||||
|
||||
This makes for smaller diffs (e.g., if `a_function_call` is renamed in the above
|
||||
example) and less rightward drift.
|
||||
|
||||
### Trailing commas
|
||||
|
||||
Lists should have a trailing comma when followed by a newline:
|
||||
|
||||
```rust
|
||||
function_call(
|
||||
argument,
|
||||
another_argument,
|
||||
);
|
||||
|
||||
let array = [
|
||||
element,
|
||||
another_element,
|
||||
yet_another_element,
|
||||
];
|
||||
```
|
||||
|
||||
This makes moving code (e.g., by copy and paste) easier, and makes diffs
|
||||
smaller, as appending or removing items does not require modifying another line
|
||||
to add or remove a comma.
|
||||
|
||||
### Blank lines
|
||||
|
||||
@ -48,11 +87,7 @@ fn bar() {}
|
||||
fn baz() {}
|
||||
```
|
||||
|
||||
Formatting tools should make the bounds on blank lines configurable: there
|
||||
should be separate minimum and maximum numbers of newlines between both
|
||||
statements and (top-level) items (i.e., four options). As described above, the
|
||||
defaults for both statements and items should be minimum: 1, maximum: 2.
|
||||
|
||||
Formatting tools may wish to make the bounds on blank lines configurable.
|
||||
|
||||
### [Module-level items](items.md)
|
||||
### [Statements](statements.md)
|
||||
|
@ -2,10 +2,10 @@
|
||||
|
||||
[Introduction](README.md)
|
||||
|
||||
- [Module-level items](items.md)
|
||||
- [Items](items.md)
|
||||
- [Statements](statements.md)
|
||||
- [Expressions](expressions.md)
|
||||
- [Types](types.md)
|
||||
- [Non-formatting conventions](advice.md)
|
||||
- [Types and Bounds](types.md)
|
||||
- [Other style advice](advice.md)
|
||||
- [`Cargo.toml` conventions](cargo.md)
|
||||
- [Principles used for deciding these guidelines](principles.md)
|
||||
- [Guiding principles and rationale](principles.md)
|
||||
|
@ -25,9 +25,9 @@ if y {
|
||||
* 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_`), or use raw
|
||||
identifiers if possible.
|
||||
* When a name is forbidden because it is a reserved word (such as `crate`),
|
||||
either use a raw identifier (`r#crate`) or use a trailing underscore
|
||||
(`crate_`). Don't misspell the word (`krate`).
|
||||
|
||||
### Modules
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
# Cargo.toml conventions
|
||||
# `Cargo.toml` conventions
|
||||
|
||||
## Formatting conventions
|
||||
|
||||
@ -25,16 +25,17 @@ not indent any key names; start all key names at the start of a line.
|
||||
Use multi-line strings (rather than newline escape sequences) for any string
|
||||
values that include multiple lines, such as the crate description.
|
||||
|
||||
For array values, such as a list of authors, put the entire list on the same
|
||||
For array values, such as a list of features, put the entire list on the same
|
||||
line as the key, if it fits. Otherwise, use block indentation: put a newline
|
||||
after the opening square bracket, indent each item by one indentation level,
|
||||
put a comma after each item (including the last), and put the closing square
|
||||
bracket at the start of a line by itself after the last item.
|
||||
|
||||
```rust
|
||||
authors = [
|
||||
"A Uthor <a.uthor@example.org>",
|
||||
"Another Author <author@example.net>",
|
||||
some_feature = [
|
||||
"another_feature",
|
||||
"yet_another_feature",
|
||||
"some_dependency?/some_feature",
|
||||
]
|
||||
```
|
||||
|
||||
@ -54,11 +55,11 @@ version = "4.5.6"
|
||||
|
||||
## Metadata conventions
|
||||
|
||||
The authors list should consist of strings that each contain an author name
|
||||
followed by an email address in angle brackets: `Full Name <email@address>`.
|
||||
It should not contain bare email addresses, or names without email addresses.
|
||||
(The authors list may also include a mailing list address without an associated
|
||||
name.)
|
||||
The authors list, if present, should consist of strings that each contain an
|
||||
author name followed by an email address in angle brackets: `Full Name
|
||||
<email@address>`. It should not contain bare email addresses, or names without
|
||||
email addresses. (The authors list may also include a mailing list address
|
||||
without an associated name.)
|
||||
|
||||
The license field must contain a valid [SPDX
|
||||
expression](https://spdx.org/spdx-specification-21-web-version#h.jxpfx0ykyb60),
|
||||
|
@ -1,5 +1,10 @@
|
||||
## Items
|
||||
|
||||
Items consist of the set of things permitted at the top level of a module.
|
||||
However, Rust also allows some items to appear within some other types of
|
||||
items, such as within a function. The same formatting conventions apply whether
|
||||
an item appears at module level or within another item.
|
||||
|
||||
`extern crate` statements must be first in a file. They must be ordered
|
||||
alphabetically.
|
||||
|
||||
@ -15,8 +20,8 @@ Tools should make the above ordering optional.
|
||||
|
||||
### Function definitions
|
||||
|
||||
In Rust, one finds functions by searching for `fn [function-name]`; It's
|
||||
important that you style your code so that it's very searchable in this way.
|
||||
In Rust, people often find functions by searching for `fn [function-name]`, so
|
||||
the formatting of function definitions shold enable this.
|
||||
|
||||
The proper ordering and spacing is:
|
||||
|
||||
@ -63,8 +68,9 @@ let y = (11, 22, 33);
|
||||
|
||||
In the declaration, put each variant on its own line, block indented.
|
||||
|
||||
Format each variant accordingly as either a struct, tuple struct, or identifier,
|
||||
which doesn't require special formatting (but without the `struct` keyword.
|
||||
Format each variant accordingly as either a struct (but without the `struct`
|
||||
keyword), a tuple struct, or an identifier (which doesn't require special
|
||||
formatting):
|
||||
|
||||
```rust
|
||||
enum FooBar {
|
||||
@ -139,7 +145,7 @@ union Foo {
|
||||
|
||||
Put the whole struct on one line if possible. Types in the parentheses should be
|
||||
separated by a comma and space with no trailing comma. No spaces around the
|
||||
parentheses or semi-colon:
|
||||
parentheses or semicolon:
|
||||
|
||||
```rust
|
||||
pub struct Foo(String, u8);
|
||||
@ -230,7 +236,7 @@ impl Bar
|
||||
|
||||
`extern crate foo;`
|
||||
|
||||
Use spaces around keywords, no spaces around the semi-colon.
|
||||
Use spaces around keywords, no spaces around the semicolon.
|
||||
|
||||
|
||||
### Modules
|
||||
@ -245,7 +251,7 @@ mod foo;
|
||||
```
|
||||
|
||||
Use spaces around keywords and before the opening brace, no spaces around the
|
||||
semi-colon.
|
||||
semicolon.
|
||||
|
||||
### macro\_rules!
|
||||
|
||||
@ -478,8 +484,8 @@ foo::{
|
||||
A *group* of imports is a set of imports on the same or sequential lines. One or
|
||||
more blank lines or other items (e.g., a function) separate groups of imports.
|
||||
|
||||
Within a group of imports, imports must be sorted ascii-betically. Groups of
|
||||
imports must not be merged or re-ordered.
|
||||
Within a group of imports, imports must be sorted ASCIIbetically (uppercase
|
||||
before lowercase). Groups of imports must not be merged or re-ordered.
|
||||
|
||||
|
||||
E.g., input:
|
||||
@ -505,13 +511,9 @@ use b;
|
||||
Because of `macro_use`, attributes must also start a new group and prevent
|
||||
re-ordering.
|
||||
|
||||
Note that tools which only have access to syntax (such as Rustfmt) cannot tell
|
||||
which imports are from an external crate or the std lib, etc.
|
||||
|
||||
|
||||
#### Ordering list import
|
||||
|
||||
Names in a list import must be sorted ascii-betically, but with `self` and
|
||||
Names in a list import must be sorted ASCIIbetically, but with `self` and
|
||||
`super` first, and groups and glob imports last. This applies recursively. For
|
||||
example, `a::*` comes before `b::a` but `a::b` comes before `a::*`. E.g.,
|
||||
`use foo::bar::{a, b::c, b::d, b::d::{x, y, z}, b::{self, r, s}};`.
|
||||
|
@ -1,7 +1,7 @@
|
||||
# Guiding principles and rationale
|
||||
|
||||
When deciding on style guidelines, the style team tried to be guided by the
|
||||
following principles (in rough priority order):
|
||||
When deciding on style guidelines, the style team follows these guiding
|
||||
principles (in rough priority order):
|
||||
|
||||
* readability
|
||||
- scan-ability
|
||||
@ -19,35 +19,11 @@ following principles (in rough priority order):
|
||||
* specifics
|
||||
- compatibility with version control practices - preserving diffs,
|
||||
merge-friendliness, etc.
|
||||
- preventing right-ward drift
|
||||
- preventing rightward drift
|
||||
- minimising vertical space
|
||||
|
||||
* application
|
||||
- ease of manual application
|
||||
- ease of implementation (in Rustfmt, and in other tools/editors/code generators)
|
||||
- ease of implementation (in `rustfmt`, and in other tools/editors/code generators)
|
||||
- internal consistency
|
||||
- simplicity of formatting rules
|
||||
|
||||
|
||||
## Overarching guidelines
|
||||
|
||||
Prefer block indent over visual indent. E.g.,
|
||||
|
||||
```rust
|
||||
// Block indent
|
||||
a_function_call(
|
||||
foo,
|
||||
bar,
|
||||
);
|
||||
|
||||
// Visual indent
|
||||
a_function_call(foo,
|
||||
bar);
|
||||
```
|
||||
|
||||
This makes for smaller diffs (e.g., if `a_function_call` is renamed in the above
|
||||
example) and less rightward drift.
|
||||
|
||||
Lists should have a trailing comma when followed by a newline, see the block
|
||||
indent example above. This choice makes moving code (e.g., by copy and paste)
|
||||
easier and makes smaller diffs.
|
||||
|
@ -1,7 +1,9 @@
|
||||
## Statements
|
||||
|
||||
### Let statements
|
||||
|
||||
There should be spaces after the `:` and on both sides of the `=` (if they are
|
||||
present). No space before the semi-colon.
|
||||
present). No space before the semicolon.
|
||||
|
||||
```rust
|
||||
// A comment.
|
||||
@ -194,7 +196,7 @@ used to determine whether a let-else statement is *short*.
|
||||
### Macros in statement position
|
||||
|
||||
A macro use in statement position should use parentheses or square brackets as
|
||||
delimiters and should be terminated with a semi-colon. There should be no spaces
|
||||
delimiters and should be terminated with a semicolon. There should be no spaces
|
||||
between the name, `!`, the delimiters, or the `;`.
|
||||
|
||||
```rust
|
||||
@ -205,13 +207,13 @@ a_macro!(...);
|
||||
|
||||
### Expressions in statement position
|
||||
|
||||
There should be no space between the expression and the semi-colon.
|
||||
There should be no space between the expression and the semicolon.
|
||||
|
||||
```
|
||||
<expr>;
|
||||
```
|
||||
|
||||
All expressions in statement position should be terminated with a semi-colon,
|
||||
All expressions in statement position should be terminated with a semicolon,
|
||||
unless they end with a block or are used as the value for a block.
|
||||
|
||||
E.g.,
|
||||
@ -229,7 +231,7 @@ loop {
|
||||
}
|
||||
```
|
||||
|
||||
Use a semi-colon where an expression has void type, even if it could be
|
||||
Use a semicolon where an expression has void type, even if it could be
|
||||
propagated. E.g.,
|
||||
|
||||
```rust
|
||||
|
Loading…
Reference in New Issue
Block a user