Skip to content

Commit e2b1736

Browse files
committed
Loops grammar
1 parent 857f2c9 commit e2b1736

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/expressions/loop-expr.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
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; | [_IteratorLoopExpression_]
9+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_WhileLetExpression_]
10+
> &nbsp;&nbsp; )
11+
12+
[_LoopLabel_]: #loop-labels
13+
[_InfiniteLoopExpression_]: #infinite-loops
14+
[_PredicateLoopExpression_]: #predicate-loops
15+
[_IteratorLoopExpression_]: #iterator-loops
16+
[_WhileLetExpression_]: #while-let-loops
17+
318
Rust supports four loop expressions:
419

520
* A [`loop` expression](#infinite-loops) denotes an infinite loop.
@@ -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
@@ -44,9 +67,14 @@ while i < 10 {
4467

4568
## `while let` loops
4669

70+
> **<sup>Syntax</sup>**
71+
> [_WhileLetExpression_] :
72+
> &nbsp;&nbsp; `while` `let` _Pattern_ `=` [_Expression_]<sub>except struct expression</sub>
73+
> [_BlockExpression_]
74+
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
77+
pattern, an `=` and a block expression. If the value of the expression on the right
5078
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.
@@ -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)