Skip to content

Commit bbec462

Browse files
authored
Merge pull request rust-lang#139 from brauliobz/grammar_loop_expr
Loops grammar
2 parents c5ef18c + 413fe21 commit bbec462

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

src/expressions/loop-expr.md

+56-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
# Loops
22

3+
> **<sup>Syntax</sup>**
4+
> _LoopExpression_ :
5+
> &nbsp;&nbsp; [_LoopLabel_]<sup>?</sup> (
6+
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_InfiniteLoopExpression_]
7+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_PredicateLoopExpression_]
8+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_PredicatePatternLoopExpression_]
9+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_IteratorLoopExpression_]
10+
> &nbsp;&nbsp; )
11+
12+
[_LoopLabel_]: #loop-labels
13+
[_InfiniteLoopExpression_]: #infinite-loops
14+
[_PredicateLoopExpression_]: #predicate-loops
15+
[_PredicatePatternLoopExpression_]: #predicate-pattern-loops
16+
[_IteratorLoopExpression_]: #iterator-loops
17+
318
Rust supports four loop expressions:
419

520
* A [`loop` expression](#infinite-loops) denotes an infinite loop.
621
* A [`while` expression](#predicate-loops) loops until a predicate is false.
7-
* A [`while let` expression](#while-let-loops) tests a refutable pattern.
22+
* A [`while let` expression](#predicate-pattern-loops) tests a refutable pattern.
823
* A [`for` expression](#iterator-loops) extracts values from an iterator,
924
looping until the iterator is empty.
1025

@@ -14,6 +29,10 @@ Only `loop` supports [evaluation to non-trivial values](#break-and-loop-values).
1429

1530
## Infinite loops
1631

32+
> **<sup>Syntax</sup>**
33+
> _InfiniteLoopExpression_ :
34+
> &nbsp;&nbsp; `loop` [_BlockExpression_]
35+
1736
A `loop` expression repeats execution of its body continuously:
1837
`loop { println!("I live."); }`.
1938

@@ -26,6 +45,10 @@ expression(s).
2645

2746
## Predicate loops
2847

48+
> **<sup>Syntax</sup>**
49+
> _PredicateLoopExpression_ :
50+
> &nbsp;&nbsp; `while` [_Expression_]<sub>except struct expression</sub> [_BlockExpression_]
51+
2952
A `while` loop begins by evaluating the boolean loop conditional expression. If
3053
the loop conditional expression evaluates to `true`, the loop body block
3154
executes, then control returns to the loop conditional expression. If the loop
@@ -42,12 +65,17 @@ while i < 10 {
4265
}
4366
```
4467

45-
## `while let` loops
68+
## Predicate pattern loops
69+
70+
> **<sup>Syntax</sup>**
71+
> [_PredicatePatternLoopExpression_] :
72+
> &nbsp;&nbsp; `while` `let` _Pattern_ `=` [_Expression_]<sub>except struct expression</sub>
73+
> [_BlockExpression_]
4674
4775
A `while let` loop is semantically similar to a `while` loop but in place of a
4876
condition expression it expects the keyword `let` followed by a refutable
49-
pattern, an `=` and an expression. If the value of the expression on the right
50-
hand side of the `=` matches the pattern, the loop body block executes then
77+
pattern, an `=`, an expression and a block expression. If the value of the expression on
78+
the right hand side of the `=` matches the pattern, the loop body block executes then
5179
control returns to the pattern matching statement. Otherwise, the while
5280
expression completes.
5381

@@ -61,6 +89,11 @@ while let Some(y) = x.pop() {
6189

6290
## Iterator loops
6391

92+
> **<sup>Syntax</sup>**
93+
> _IteratorLoopExpression_ :
94+
> &nbsp;&nbsp; `for` _Pattern_ `in` [_Expression_]<sub>except struct expression</sub>
95+
> [_BlockExpression_]
96+
6497
A `for` expression is a syntactic construct for looping over elements provided
6598
by an implementation of `std::iter::IntoIterator`. If the iterator yields a
6699
value, that value is given the specified name and the body of the loop is
@@ -89,6 +122,10 @@ assert_eq!(sum, 55);
89122

90123
## Loop labels
91124

125+
> **<sup>Syntax</sup>**
126+
> _LoopLabel_ :
127+
> &nbsp;&nbsp; [LIFETIME_OR_LABEL] `:`
128+
92129
A loop expression may optionally have a _label_. The label is written as
93130
a lifetime preceding the loop expression, as in `'foo: loop { break 'foo; }`,
94131
`'bar: while false {}`, `'humbug: for _ in 0..0 {}`.
@@ -99,6 +136,10 @@ expressions](#continue-expressions).
99136

100137
## `break` expressions
101138

139+
> **<sup>Syntax</sup>**
140+
> _BreakExpression_ :
141+
> &nbsp;&nbsp; `break` [LIFETIME_OR_LABEL]<sup>?</sup> [_Expression_]<sup>?</sup>
142+
102143
When `break` is encountered, execution of the associated loop body is
103144
immediately terminated, for example:
104145

@@ -131,6 +172,10 @@ the forms `break`, `break 'label` or ([see below](#break-and-loop-values))
131172

132173
## `continue` expressions
133174

175+
> **<sup>Syntax</sup>**
176+
> _ContinueExpression_ :
177+
> &nbsp;&nbsp; `continue` [LIFETIME_OR_LABEL]<sup>?</sup>
178+
134179
When `continue` is encountered, the current iteration of the associated loop
135180
body is immediately terminated, returning control to the loop *head*. In
136181
the case of a `while` loop, the head is the conditional expression controlling
@@ -165,3 +210,10 @@ In the case a `loop` has an associated `break`, it is not considered diverging,
165210
and the `loop` must have a type compatible with each `break` expression.
166211
`break` without an expression is considered identical to `break` with
167212
expression `()`.
213+
214+
[IDENTIFIER]: identifiers.html
215+
216+
[_Expression_]: expressions.html
217+
[_BlockExpression_]: expressions/block-expr.html
218+
219+
[LIFETIME_OR_LABEL]: tokens.html#symbols

0 commit comments

Comments
 (0)