Skip to content

Commit 76dd691

Browse files
committed
Rollup merge of rust-lang#24750 - withoutboats:patch-1, r=steveklabnik
1) Moved 'while' section below 'loop', 'break', and 'continue'; 2) Added information to 'while' and 'for' loops that they interact with 'break' and 'continue' and may have a lifetime label. 3) Clarified labeling syntax on the infinite loops.
2 parents 7e3bb03 + 3ae6a5e commit 76dd691

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

src/doc/reference.md

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,28 +3081,6 @@ fn ten_times<F>(f: F) where F: Fn(i32) {
30813081
ten_times(|j| println!("hello, {}", j));
30823082
```
30833083

3084-
### While loops
3085-
3086-
```{.ebnf .gram}
3087-
while_expr : [ lifetime ':' ] "while" no_struct_literal_expr '{' block '}' ;
3088-
```
3089-
3090-
A `while` loop begins by evaluating the boolean loop conditional expression.
3091-
If the loop conditional expression evaluates to `true`, the loop body block
3092-
executes and control returns to the loop conditional expression. If the loop
3093-
conditional expression evaluates to `false`, the `while` expression completes.
3094-
3095-
An example:
3096-
3097-
```
3098-
let mut i = 0;
3099-
3100-
while i < 10 {
3101-
println!("hello");
3102-
i = i + 1;
3103-
}
3104-
```
3105-
31063084
### Infinite loops
31073085

31083086
A `loop` expression denotes an infinite loop.
@@ -3111,10 +3089,11 @@ A `loop` expression denotes an infinite loop.
31113089
loop_expr : [ lifetime ':' ] "loop" '{' block '}';
31123090
```
31133091

3114-
A `loop` expression may optionally have a _label_. If a label is present, then
3115-
labeled `break` and `continue` expressions nested within this loop may exit out
3116-
of this loop or return control to its head. See [Break
3117-
expressions](#break-expressions) and [Continue
3092+
A `loop` expression may optionally have a _label_. The label is written as
3093+
a lifetime preceding the loop expression, as in `'foo: loop{ }`. If a
3094+
label is present, then labeled `break` and `continue` expressions nested
3095+
within this loop may exit out of this loop or return control to its head.
3096+
See [Break expressions](#break-expressions) and [Continue
31183097
expressions](#continue-expressions).
31193098

31203099
### Break expressions
@@ -3126,7 +3105,7 @@ break_expr : "break" [ lifetime ];
31263105
A `break` expression has an optional _label_. If the label is absent, then
31273106
executing a `break` expression immediately terminates the innermost loop
31283107
enclosing it. It is only permitted in the body of a loop. If the label is
3129-
present, then `break foo` terminates the loop with label `foo`, which need not
3108+
present, then `break 'foo` terminates the loop with label `'foo`, which need not
31303109
be the innermost label enclosing the `break` expression, but must enclose it.
31313110

31323111
### Continue expressions
@@ -3140,12 +3119,39 @@ executing a `continue` expression immediately terminates the current iteration
31403119
of the innermost loop enclosing it, returning control to the loop *head*. In
31413120
the case of a `while` loop, the head is the conditional expression controlling
31423121
the loop. In the case of a `for` loop, the head is the call-expression
3143-
controlling the loop. If the label is present, then `continue foo` returns
3144-
control to the head of the loop with label `foo`, which need not be the
3122+
controlling the loop. If the label is present, then `continue 'foo` returns
3123+
control to the head of the loop with label `'foo`, which need not be the
31453124
innermost label enclosing the `break` expression, but must enclose it.
31463125

31473126
A `continue` expression is only permitted in the body of a loop.
31483127

3128+
### While loops
3129+
3130+
```{.ebnf .gram}
3131+
while_expr : [ lifetime ':' ] "while" no_struct_literal_expr '{' block '}' ;
3132+
```
3133+
3134+
A `while` loop begins by evaluating the boolean loop conditional expression.
3135+
If the loop conditional expression evaluates to `true`, the loop body block
3136+
executes and control returns to the loop conditional expression. If the loop
3137+
conditional expression evaluates to `false`, the `while` expression completes.
3138+
3139+
An example:
3140+
3141+
```
3142+
let mut i = 0;
3143+
3144+
while i < 10 {
3145+
println!("hello");
3146+
i = i + 1;
3147+
}
3148+
```
3149+
3150+
Like `loop` expressions, `while` loops can be controlled with `break` or
3151+
`continue`, and may optionally have a _label_. See [infinite
3152+
loops](#infinite-loops), [break expressions](#break-expressions), and
3153+
[continue expressions](#continue-expressions) for more information.
3154+
31493155
### For expressions
31503156

31513157
```{.ebnf .gram}
@@ -3180,6 +3186,11 @@ for i in 0..256 {
31803186
}
31813187
```
31823188

3189+
Like `loop` expressions, `for` loops can be controlled with `break` or
3190+
`continue`, and may optionally have a _label_. See [infinite
3191+
loops](#infinite-loops), [break expressions](#break-expressions), and
3192+
[continue expressions](#continue-expressions) for more information.
3193+
31833194
### If expressions
31843195

31853196
```{.ebnf .gram}

0 commit comments

Comments
 (0)