11# Loops
22
3+ > ** <sup >Syntax</sup >**
4+ > _ LoopExpression_ :
5+ >   ;  ; [ _ LoopLabel_ ] <sup >?</sup > (
6+ >   ;  ;   ;  ;   ;  ; [ _ InfiniteLoopExpression_ ]
7+ >   ;  ;   ;  ; | [ _ PredicateLoopExpression_ ]
8+ >   ;  ;   ;  ; | [ _ PredicatePatternLoopExpression_ ]
9+ >   ;  ;   ;  ; | [ _ IteratorLoopExpression_ ]
10+ >   ;  ; )
11+
12+ [ _LoopLabel_ ] : #loop-labels
13+ [ _InfiniteLoopExpression_ ] : #infinite-loops
14+ [ _PredicateLoopExpression_ ] : #predicate-loops
15+ [ _PredicatePatternLoopExpression_ ] : #predicate-pattern-loops
16+ [ _IteratorLoopExpression_ ] : #iterator-loops
17+
318Rust 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+ >   ;  ; ` loop ` [ _ BlockExpression_ ]
35+
1736A ` 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+ >   ;  ; ` while ` [ _ Expression_ ] <sub >except struct expression</sub > [ _ BlockExpression_ ]
51+
2952A ` while ` loop begins by evaluating the boolean loop conditional expression. If
3053the loop conditional expression evaluates to ` true ` , the loop body block
3154executes, 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+ >   ;  ; ` while ` ` let ` _ Pattern_ ` = ` [ _ Expression_ ] <sub >except struct expression</sub >
73+ > [ _ BlockExpression_ ]
4674
4775A ` while let ` loop is semantically similar to a ` while ` loop but in place of a
4876condition 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
5179control returns to the pattern matching statement. Otherwise, the while
5280expression completes.
5381
@@ -61,6 +89,11 @@ while let Some(y) = x.pop() {
6189
6290## Iterator loops
6391
92+ > ** <sup >Syntax</sup >**
93+ > _ IteratorLoopExpression_ :
94+ >   ;  ; ` for ` _ Pattern_ ` in ` [ _ Expression_ ] <sub >except struct expression</sub >
95+ > [ _ BlockExpression_ ]
96+
6497A ` for ` expression is a syntactic construct for looping over elements provided
6598by an implementation of ` std::iter::IntoIterator ` . If the iterator yields a
6699value, 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+ >   ;  ; [ LIFETIME_OR_LABEL] ` : `
128+
92129A loop expression may optionally have a _ label_ . The label is written as
93130a 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+ >   ;  ; ` break ` [ LIFETIME_OR_LABEL] <sup >?</sup > [ _ Expression_ ] <sup >?</sup >
142+
102143When ` break ` is encountered, execution of the associated loop body is
103144immediately 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+ >   ;  ; ` continue ` [ LIFETIME_OR_LABEL] <sup >?</sup >
178+
134179When ` continue ` is encountered, the current iteration of the associated loop
135180body is immediately terminated, returning control to the loop * head* . In
136181the 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,
165210and the ` loop ` must have a type compatible with each ` break ` expression.
166211` break ` without an expression is considered identical to ` break ` with
167212expression ` () ` .
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