1
- ## Expressions
1
+ # Expressions
2
2
3
- ### Blocks
3
+ ## Blocks
4
4
5
5
A block expression must have a newline after the initial ` { ` and before the
6
6
terminal ` } ` , unless it qualifies to be written as a single line based on
@@ -116,8 +116,7 @@ fn main() {
116
116
}
117
117
```
118
118
119
-
120
- ### Closures
119
+ ## Closures
121
120
122
121
Don't put any extra spaces before the first ` | ` (unless the closure is prefixed
123
122
by a keyword such as ` move ` ); put a space between the second ` | ` and the
@@ -155,8 +154,7 @@ move |arg1: i32, arg2: i32| -> i32 {
155
154
}
156
155
```
157
156
158
-
159
- ### Struct literals
157
+ ## Struct literals
160
158
161
159
If a struct literal is * small* , format it on a single line, and do not use a
162
160
trailing comma. If not, split it across multiple lines, with each field on its
@@ -185,8 +183,7 @@ let f = Foo {
185
183
};
186
184
```
187
185
188
-
189
- ### Tuple literals
186
+ ## Tuple literals
190
187
191
188
Use a single-line form where possible. Do not put spaces between the opening
192
189
parenthesis and the first element, or between the last element and the closing
@@ -205,8 +202,7 @@ let x = (
205
202
);
206
203
```
207
204
208
-
209
- ### Tuple struct literals
205
+ ## Tuple struct literals
210
206
211
207
Do not put space between the identifier and the opening parenthesis. Otherwise,
212
208
follow the rules for tuple literals:
@@ -220,8 +216,7 @@ let x = Foo(
220
216
);
221
217
```
222
218
223
-
224
- ### Enum literals
219
+ ## Enum literals
225
220
226
221
Follow the formatting rules for the various struct literals. Prefer using the
227
222
name of the enum as a qualifying name, unless the enum is in the prelude:
@@ -235,8 +230,7 @@ Foo::Baz {
235
230
Ok (an_expr )
236
231
```
237
232
238
-
239
- ### Array literals
233
+ ## Array literals
240
234
241
235
Write small array literals on a single line. Do not put spaces between the opening
242
236
square bracket and the first element, or between the last element and the closing
@@ -276,8 +270,7 @@ fn main() {
276
270
}
277
271
```
278
272
279
-
280
- ### Array accesses, indexing, and slicing.
273
+ ## Array accesses, indexing, and slicing
281
274
282
275
Don't put spaces around the square brackets. Avoid breaking lines if possible.
283
276
Never break a line between the target expression and the opening square
@@ -300,13 +293,13 @@ fn main() {
300
293
}
301
294
```
302
295
303
- ### Unary operations
296
+ ## Unary operations
304
297
305
298
Do not include a space between a unary op and its operand (i.e., ` !x ` , not
306
299
` ! x ` ). However, there must be a space after ` &mut ` . Avoid line-breaking
307
300
between a unary operator and its operand.
308
301
309
- ### Binary operations
302
+ ## Binary operations
310
303
311
304
Do include spaces around binary ops (i.e., ` x + 1 ` , not ` x+1 ` ) (including ` = `
312
305
and other assignment operators such as ` += ` or ` *= ` ).
@@ -335,7 +328,7 @@ foo_bar
335
328
Prefer line-breaking at an assignment operator (either ` = ` or ` += ` , etc.) rather
336
329
than at other binary operators.
337
330
338
- ### Control flow
331
+ ## Control flow
339
332
340
333
Do not include extraneous parentheses for ` if ` and ` while ` expressions.
341
334
@@ -354,7 +347,7 @@ if (true) {
354
347
Do include extraneous parentheses if it makes an arithmetic or logic expression
355
348
easier to understand (` (x * 15) + (y * 20) ` is fine)
356
349
357
- ### Function calls
350
+ ## Function calls
358
351
359
352
Do not put a space between the function name, and the opening parenthesis.
360
353
@@ -364,7 +357,7 @@ Do put a space between an argument, and the comma which precedes it.
364
357
365
358
Prefer not to break a line in the callee expression.
366
359
367
- #### Single-line calls
360
+ ### Single-line calls
368
361
369
362
Do not put a space between the function name and open paren, between the open
370
363
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.
375
368
foo (x , y , z )
376
369
```
377
370
378
- #### Multi-line calls
371
+ ### Multi-line calls
379
372
380
373
If the function call is not * small* , it would otherwise over-run the max width,
381
374
or any argument or the callee is multi-line, then format the call across
@@ -390,8 +383,7 @@ a_function_call(
390
383
)
391
384
```
392
385
393
-
394
- ### Method calls
386
+ ## Method calls
395
387
396
388
Follow the function rules for calling.
397
389
@@ -401,15 +393,14 @@ Do not put any spaces around the `.`.
401
393
x . foo (). bar (). baz (x , y , z );
402
394
```
403
395
404
-
405
- ### Macro uses
396
+ ## Macro uses
406
397
407
398
If a macro can be parsed like other constructs, format it like those
408
399
constructs. For example, a macro use ` foo!(a, b, c) ` can be parsed like a
409
400
function call (ignoring the ` ! ` ), so format it using the rules for function
410
401
calls.
411
402
412
- #### Special case macros
403
+ ### Special case macros
413
404
414
405
For macros which take a format string, if all other arguments are * small* ,
415
406
format the arguments before the format string on a single line if they fit, and
@@ -430,17 +421,15 @@ assert_eq!(
430
421
);
431
422
```
432
423
433
-
434
- ### Casts (` as ` )
424
+ ## Casts (` as ` )
435
425
436
426
Put spaces before and after ` as ` :
437
427
438
428
``` rust
439
429
let cstr = " Hi\ 0" as * const str as * const [u8 ] as * const std :: os :: raw :: c_char ;
440
430
```
441
431
442
-
443
- ### Chains of fields and method calls
432
+ ## Chains of fields and method calls
444
433
445
434
A chain is a sequence of field accesses, method calls, and/or uses of the try
446
435
operator ` ? ` . E.g., ` a.b.c().d ` or ` foo?.bar().baz? ` .
478
467
. qux ();
479
468
```
480
469
481
- #### Multi-line elements
470
+ ### Multi-line elements
482
471
483
472
If any element in a chain is formatted across multiple lines, put that element
484
473
and any later elements on their own lines.
@@ -513,7 +502,7 @@ self.pre_comment.as_ref().map_or(
513
502
)
514
503
```
515
504
516
- ### Control flow expressions
505
+ ## Control flow expressions
517
506
518
507
This section covers ` if ` , ` if let ` , ` loop ` , ` while ` , ` while let ` , and ` for `
519
508
expressions.
@@ -584,8 +573,7 @@ if !self.config.file_lines().intersects(
584
573
}
585
574
```
586
575
587
-
588
- #### Single line ` if else `
576
+ ### Single line ` if else `
589
577
590
578
Put an ` if else ` or ` if let else ` on a single line if it occurs in expression
591
579
context (i.e., is not a standalone statement), it contains a single ` else `
@@ -608,8 +596,7 @@ if x {
608
596
}
609
597
```
610
598
611
-
612
- ### Match
599
+ ## Match
613
600
614
601
Prefer not to line-break inside the discriminant expression. Always break after
615
602
the opening brace and before the closing brace. Block-indent the match arms
@@ -718,7 +705,7 @@ match foo {
718
705
}
719
706
```
720
707
721
- #### Line-breaking
708
+ ### Line-breaking
722
709
723
710
If using a block form on the right-hand side of a match arm makes it possible
724
711
to avoid breaking on the left-hand side, do that:
@@ -812,8 +799,7 @@ small_no_tuple:
812
799
813
800
E.g., ` &&Some(foo) ` matches, ` Foo(4, Bar) ` does not.
814
801
815
-
816
- ### Combinable expressions
802
+ ## Combinable expressions
817
803
818
804
Where a function call has a single argument, and that argument is formatted
819
805
across multiple-lines, format the outer call as if it were a single-line call,
@@ -861,8 +847,7 @@ foo(first_arg, x, |param| {
861
847
})
862
848
```
863
849
864
-
865
- ### Ranges
850
+ ## Ranges
866
851
867
852
Do not put spaces in ranges, e.g., ` 0..10 ` , ` x..=y ` , ` ..x.len() ` , ` foo.. ` .
868
853
@@ -879,8 +864,7 @@ For the sake of indicating precedence, if either bound is a compound
879
864
expression, use parentheses around it, e.g., ` ..(x + 1) ` , ` (x.f)..(x.f.len()) ` ,
880
865
or ` 0..(x - 10) ` .
881
866
882
-
883
- ### Hexadecimal literals
867
+ ## Hexadecimal literals
884
868
885
869
Hexadecimal literals may use upper- or lower-case letters, but they must not be
886
870
mixed within the same literal. Projects should use the same case for all
0 commit comments