Skip to content

Commit b4b540c

Browse files
committed
Improve the rust style guide doc
- Make the levels of headings consistent in this whole document - Fix some headings - Remove redundant empty lines - Follow the markdown linter advices to use the same symbol for different level of unordered list entries
1 parent 89acdae commit b4b540c

File tree

8 files changed

+98
-134
lines changed

8 files changed

+98
-134
lines changed

Diff for: src/doc/style-guide/src/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,12 @@ fn baz() {}
100100
```
101101

102102
### [Module-level items](items.md)
103+
103104
### [Statements](statements.md)
105+
104106
### [Expressions](expressions.md)
105-
### [Types](types.md)
106107

108+
### [Types](types.md)
107109

108110
### Comments
109111

Diff for: src/doc/style-guide/src/SUMMARY.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
[Introduction](README.md)
44

5-
- [Items](items.md)
6-
- [Statements](statements.md)
7-
- [Expressions](expressions.md)
8-
- [Types and Bounds](types.md)
9-
- [Other style advice](advice.md)
10-
- [`Cargo.toml` conventions](cargo.md)
11-
- [Guiding principles and rationale](principles.md)
12-
- [Nightly-only syntax](nightly.md)
5+
* [Items](items.md)
6+
* [Statements](statements.md)
7+
* [Expressions](expressions.md)
8+
* [Types and Bounds](types.md)
9+
* [Other style advice](advice.md)
10+
* [`Cargo.toml` conventions](cargo.md)
11+
* [Guiding principles and rationale](principles.md)
12+
* [Nightly-only syntax](nightly.md)

Diff for: src/doc/style-guide/src/advice.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ if y {
1818

1919
## Names
2020

21-
* Types shall be `UpperCamelCase`,
22-
* Enum variants shall be `UpperCamelCase`,
23-
* Struct fields shall be `snake_case`,
24-
* Function and method names shall be `snake_case`,
25-
* Local variables shall be `snake_case`,
26-
* Macro names shall be `snake_case`,
27-
* Constants (`const`s and immutable `static`s) shall be `SCREAMING_SNAKE_CASE`.
28-
* When a name is forbidden because it is a reserved word (such as `crate`),
29-
either use a raw identifier (`r#crate`) or use a trailing underscore
30-
(`crate_`). Don't misspell the word (`krate`).
21+
* Types shall be `UpperCamelCase`,
22+
* Enum variants shall be `UpperCamelCase`,
23+
* Struct fields shall be `snake_case`,
24+
* Function and method names shall be `snake_case`,
25+
* Local variables shall be `snake_case`,
26+
* Macro names shall be `snake_case`,
27+
* Constants (`const`s and immutable `static`s) shall be `SCREAMING_SNAKE_CASE`.
28+
* When a name is forbidden because it is a reserved word (such as `crate`),
29+
either use a raw identifier (`r#crate`) or use a trailing underscore
30+
(`crate_`). Don't misspell the word (`krate`).
3131

3232
### Modules
3333

Diff for: src/doc/style-guide/src/expressions.md

+28-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## Expressions
1+
# Expressions
22

3-
### Blocks
3+
## Blocks
44

55
A block expression must have a newline after the initial `{` and before the
66
terminal `}`, unless it qualifies to be written as a single line based on
@@ -116,8 +116,7 @@ fn main() {
116116
}
117117
```
118118

119-
120-
### Closures
119+
## Closures
121120

122121
Don't put any extra spaces before the first `|` (unless the closure is prefixed
123122
by a keyword such as `move`); put a space between the second `|` and the
@@ -155,8 +154,7 @@ move |arg1: i32, arg2: i32| -> i32 {
155154
}
156155
```
157156

158-
159-
### Struct literals
157+
## Struct literals
160158

161159
If a struct literal is *small*, format it on a single line, and do not use a
162160
trailing comma. If not, split it across multiple lines, with each field on its
@@ -185,8 +183,7 @@ let f = Foo {
185183
};
186184
```
187185

188-
189-
### Tuple literals
186+
## Tuple literals
190187

191188
Use a single-line form where possible. Do not put spaces between the opening
192189
parenthesis and the first element, or between the last element and the closing
@@ -205,8 +202,7 @@ let x = (
205202
);
206203
```
207204

208-
209-
### Tuple struct literals
205+
## Tuple struct literals
210206

211207
Do not put space between the identifier and the opening parenthesis. Otherwise,
212208
follow the rules for tuple literals:
@@ -220,8 +216,7 @@ let x = Foo(
220216
);
221217
```
222218

223-
224-
### Enum literals
219+
## Enum literals
225220

226221
Follow the formatting rules for the various struct literals. Prefer using the
227222
name of the enum as a qualifying name, unless the enum is in the prelude:
@@ -235,8 +230,7 @@ Foo::Baz {
235230
Ok(an_expr)
236231
```
237232

238-
239-
### Array literals
233+
## Array literals
240234

241235
Write small array literals on a single line. Do not put spaces between the opening
242236
square bracket and the first element, or between the last element and the closing
@@ -276,8 +270,7 @@ fn main() {
276270
}
277271
```
278272

279-
280-
### Array accesses, indexing, and slicing.
273+
## Array accesses, indexing, and slicing
281274

282275
Don't put spaces around the square brackets. Avoid breaking lines if possible.
283276
Never break a line between the target expression and the opening square
@@ -300,13 +293,13 @@ fn main() {
300293
}
301294
```
302295

303-
### Unary operations
296+
## Unary operations
304297

305298
Do not include a space between a unary op and its operand (i.e., `!x`, not
306299
`! x`). However, there must be a space after `&mut`. Avoid line-breaking
307300
between a unary operator and its operand.
308301

309-
### Binary operations
302+
## Binary operations
310303

311304
Do include spaces around binary ops (i.e., `x + 1`, not `x+1`) (including `=`
312305
and other assignment operators such as `+=` or `*=`).
@@ -335,7 +328,7 @@ foo_bar
335328
Prefer line-breaking at an assignment operator (either `=` or `+=`, etc.) rather
336329
than at other binary operators.
337330

338-
### Control flow
331+
## Control flow
339332

340333
Do not include extraneous parentheses for `if` and `while` expressions.
341334

@@ -354,7 +347,7 @@ if (true) {
354347
Do include extraneous parentheses if it makes an arithmetic or logic expression
355348
easier to understand (`(x * 15) + (y * 20)` is fine)
356349

357-
### Function calls
350+
## Function calls
358351

359352
Do not put a space between the function name, and the opening parenthesis.
360353

@@ -364,7 +357,7 @@ Do put a space between an argument, and the comma which precedes it.
364357

365358
Prefer not to break a line in the callee expression.
366359

367-
#### Single-line calls
360+
### Single-line calls
368361

369362
Do not put a space between the function name and open paren, between the open
370363
paren and the first argument, or between the last argument and the close paren.
@@ -375,7 +368,7 @@ Do not put a comma after the last argument.
375368
foo(x, y, z)
376369
```
377370

378-
#### Multi-line calls
371+
### Multi-line calls
379372

380373
If the function call is not *small*, it would otherwise over-run the max width,
381374
or any argument or the callee is multi-line, then format the call across
@@ -390,8 +383,7 @@ a_function_call(
390383
)
391384
```
392385

393-
394-
### Method calls
386+
## Method calls
395387

396388
Follow the function rules for calling.
397389

@@ -401,15 +393,14 @@ Do not put any spaces around the `.`.
401393
x.foo().bar().baz(x, y, z);
402394
```
403395

404-
405-
### Macro uses
396+
## Macro uses
406397

407398
If a macro can be parsed like other constructs, format it like those
408399
constructs. For example, a macro use `foo!(a, b, c)` can be parsed like a
409400
function call (ignoring the `!`), so format it using the rules for function
410401
calls.
411402

412-
#### Special case macros
403+
### Special case macros
413404

414405
For macros which take a format string, if all other arguments are *small*,
415406
format the arguments before the format string on a single line if they fit, and
@@ -430,17 +421,15 @@ assert_eq!(
430421
);
431422
```
432423

433-
434-
### Casts (`as`)
424+
## Casts (`as`)
435425

436426
Put spaces before and after `as`:
437427

438428
```rust
439429
let cstr = "Hi\0" as *const str as *const [u8] as *const std::os::raw::c_char;
440430
```
441431

442-
443-
### Chains of fields and method calls
432+
## Chains of fields and method calls
444433

445434
A chain is a sequence of field accesses, method calls, and/or uses of the try
446435
operator `?`. E.g., `a.b.c().d` or `foo?.bar().baz?`.
@@ -478,7 +467,7 @@ foo(
478467
.qux();
479468
```
480469

481-
#### Multi-line elements
470+
### Multi-line elements
482471

483472
If any element in a chain is formatted across multiple lines, put that element
484473
and any later elements on their own lines.
@@ -513,7 +502,7 @@ self.pre_comment.as_ref().map_or(
513502
)
514503
```
515504

516-
### Control flow expressions
505+
## Control flow expressions
517506

518507
This section covers `if`, `if let`, `loop`, `while`, `while let`, and `for`
519508
expressions.
@@ -584,8 +573,7 @@ if !self.config.file_lines().intersects(
584573
}
585574
```
586575

587-
588-
#### Single line `if else`
576+
### Single line `if else`
589577

590578
Put an `if else` or `if let else` on a single line if it occurs in expression
591579
context (i.e., is not a standalone statement), it contains a single `else`
@@ -608,8 +596,7 @@ if x {
608596
}
609597
```
610598

611-
612-
### Match
599+
## Match
613600

614601
Prefer not to line-break inside the discriminant expression. Always break after
615602
the opening brace and before the closing brace. Block-indent the match arms
@@ -718,7 +705,7 @@ match foo {
718705
}
719706
```
720707

721-
#### Line-breaking
708+
### Line-breaking
722709

723710
If using a block form on the right-hand side of a match arm makes it possible
724711
to avoid breaking on the left-hand side, do that:
@@ -812,8 +799,7 @@ small_no_tuple:
812799

813800
E.g., `&&Some(foo)` matches, `Foo(4, Bar)` does not.
814801

815-
816-
### Combinable expressions
802+
## Combinable expressions
817803

818804
Where a function call has a single argument, and that argument is formatted
819805
across multiple-lines, format the outer call as if it were a single-line call,
@@ -861,8 +847,7 @@ foo(first_arg, x, |param| {
861847
})
862848
```
863849

864-
865-
### Ranges
850+
## Ranges
866851

867852
Do not put spaces in ranges, e.g., `0..10`, `x..=y`, `..x.len()`, `foo..`.
868853

@@ -879,8 +864,7 @@ For the sake of indicating precedence, if either bound is a compound
879864
expression, use parentheses around it, e.g., `..(x + 1)`, `(x.f)..(x.f.len())`,
880865
or `0..(x - 10)`.
881866

882-
883-
### Hexadecimal literals
867+
## Hexadecimal literals
884868

885869
Hexadecimal literals may use upper- or lower-case letters, but they must not be
886870
mixed within the same literal. Projects should use the same case for all

0 commit comments

Comments
 (0)