@@ -480,6 +480,101 @@ self.pre_comment.as_ref().map_or(
480
480
)
481
481
```
482
482
483
+ ### Control flow expressions
484
+
485
+ This section covers ` if ` , ` if let ` , ` loop ` , ` while ` , ` while let ` , and ` for `
486
+ expressions.
487
+
488
+ The keyword, any initial clauses, and the opening brace of the block should be
489
+ on a single line. The usual rules for block formatting should be applied to the
490
+ block.
491
+
492
+ If there is an ` else ` component, then the closing brace, ` else ` , any following
493
+ clause, and the opening brace should all be on the same line. There should be a
494
+ single space before and after the ` else ` keyword. For example:
495
+
496
+ ``` rust
497
+ if ... {
498
+ ...
499
+ } else {
500
+ ...
501
+ }
502
+
503
+ if let ... {
504
+ ...
505
+ } else if ... {
506
+ ...
507
+ } else {
508
+ ...
509
+ }
510
+ ```
511
+
512
+ If the control line needs to be broken, then prefer to break before the ` = ` in
513
+ ` * let ` expressions and before ` in ` in a ` for ` expression; the following line
514
+ should be block indented. If the control line is broken for any reason, then the
515
+ opening brace should be on its own line and not indented. Examples:
516
+
517
+ ``` rust
518
+ while let Some (foo )
519
+ = a_long_expression
520
+ {
521
+ ...
522
+ }
523
+
524
+ for foo
525
+ in a_long_expression
526
+ {
527
+ ...
528
+ }
529
+
530
+ if a_long_expression
531
+ && another_long_expression
532
+ || a_third_long_expression
533
+ {
534
+ ...
535
+ }
536
+ ```
537
+
538
+ Where the initial clause is multi-lined and ends with one or more closing
539
+ parentheses, square brackets, or braces, and there is nothing else on that line,
540
+ and that line is not indented beyond the indent on the first line of the control
541
+ flow expression, then the opening brace of the block should be put on the same
542
+ line with a preceding space. For example:
543
+
544
+ ``` rust
545
+ if ! self . config. file_lines (). intersects (
546
+ & self . codemap. lookup_line_range (
547
+ stmt . span,
548
+ ),
549
+ ) { // Opening brace on same line as initial clause.
550
+ ...
551
+ }
552
+ ```
553
+
554
+
555
+ #### Single line ` if else `
556
+
557
+ Formatters may place an ` if else ` or ` if let else ` on a single line if it occurs
558
+ in expression context (i.e., is not a standalone statement), it contains a
559
+ single ` else ` clause, and is * small* . For example:
560
+
561
+ ``` rust
562
+ let y = if x { 0 } else { 1 };
563
+
564
+ // Examples that must be multi-line.
565
+ let y = if something_very_long {
566
+ not_small
567
+ } else {
568
+ also_not_small
569
+ };
570
+
571
+ if x {
572
+ 0
573
+ } else {
574
+ 1
575
+ }
576
+ ```
577
+
483
578
484
579
### Match
485
580
@@ -664,7 +759,7 @@ Such behaviour should extend recursively, however, tools may choose to limit the
664
759
depth of nesting.
665
760
666
761
Only where the multi-line sub-expression is a closure with an explicit block,
667
- this combining behviour may be used where there are other arguments, as long as
762
+ this combining behaviour may be used where there are other arguments, as long as
668
763
all the arguments and the first line of the closure fit on the first line, the
669
764
closure is the last argument, and there is only one closure argument:
670
765
0 commit comments