Skip to content

Commit da5f80c

Browse files
author
Lee Aronson
committed
Improve information about loops
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.
1 parent 90bed3f commit da5f80c

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
@@ -3078,28 +3078,6 @@ fn ten_times<F>(f: F) where F: Fn(i32) {
30783078
ten_times(|j| println!("hello, {}", j));
30793079
```
30803080

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

31053083
A `loop` expression denotes an infinite loop.
@@ -3108,10 +3086,11 @@ A `loop` expression denotes an infinite loop.
31083086
loop_expr : [ lifetime ':' ] "loop" '{' block '}';
31093087
```
31103088

3111-
A `loop` expression may optionally have a _label_. If a label is present, then
3112-
labeled `break` and `continue` expressions nested within this loop may exit out
3113-
of this loop or return control to its head. See [Break
3114-
expressions](#break-expressions) and [Continue
3089+
A `loop` expression may optionally have a _label_. The label is written as
3090+
a lifetime preceding the loop expression, as in `'foo: loop{ }`. If a
3091+
label is present, then labeled `break` and `continue` expressions nested
3092+
within this loop may exit out of this loop or return control to its head.
3093+
See [Break expressions](#break-expressions) and [Continue
31153094
expressions](#continue-expressions).
31163095

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

31293108
### Continue expressions
@@ -3137,12 +3116,39 @@ executing a `continue` expression immediately terminates the current iteration
31373116
of the innermost loop enclosing it, returning control to the loop *head*. In
31383117
the case of a `while` loop, the head is the conditional expression controlling
31393118
the loop. In the case of a `for` loop, the head is the call-expression
3140-
controlling the loop. If the label is present, then `continue foo` returns
3141-
control to the head of the loop with label `foo`, which need not be the
3119+
controlling the loop. If the label is present, then `continue 'foo` returns
3120+
control to the head of the loop with label `'foo`, which need not be the
31423121
innermost label enclosing the `break` expression, but must enclose it.
31433122

31443123
A `continue` expression is only permitted in the body of a loop.
31453124

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

31483154
```{.ebnf .gram}
@@ -3177,6 +3183,11 @@ for i in 0..256 {
31773183
}
31783184
```
31793185

3186+
Like `loop` expressions, `while` loops can be controlled with `break` or
3187+
`continue`, and may optionally have a _label_. See [infinite
3188+
loops](#infinite-loops), [break expressions](#break-expressions), and
3189+
[continue expressions](#continue-expressions) for more information.
3190+
31803191
### If expressions
31813192

31823193
```{.ebnf .gram}

0 commit comments

Comments
 (0)