1
1
# Loops
2
2
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
+
3
18
Rust supports four loop expressions:
4
19
5
20
* A [ ` loop ` expression] ( #infinite-loops ) denotes an infinite loop.
6
21
* 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.
8
23
* A [ ` for ` expression] ( #iterator-loops ) extracts values from an iterator,
9
24
looping until the iterator is empty.
10
25
@@ -14,6 +29,10 @@ Only `loop` supports [evaluation to non-trivial values](#break-and-loop-values).
14
29
15
30
## Infinite loops
16
31
32
+ > ** <sup >Syntax</sup >**
33
+ > _ InfiniteLoopExpression_ :
34
+ >   ;  ; ` loop ` [ _ BlockExpression_ ]
35
+
17
36
A ` loop ` expression repeats execution of its body continuously:
18
37
` loop { println!("I live."); } ` .
19
38
@@ -26,6 +45,10 @@ expression(s).
26
45
27
46
## Predicate loops
28
47
48
+ > ** <sup >Syntax</sup >**
49
+ > _ PredicateLoopExpression_ :
50
+ >   ;  ; ` while ` [ _ Expression_ ] <sub >except struct expression</sub > [ _ BlockExpression_ ]
51
+
29
52
A ` while ` loop begins by evaluating the boolean loop conditional expression. If
30
53
the loop conditional expression evaluates to ` true ` , the loop body block
31
54
executes, then control returns to the loop conditional expression. If the loop
@@ -42,12 +65,17 @@ while i < 10 {
42
65
}
43
66
```
44
67
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_ ]
46
74
47
75
A ` while let ` loop is semantically similar to a ` while ` loop but in place of a
48
76
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
51
79
control returns to the pattern matching statement. Otherwise, the while
52
80
expression completes.
53
81
@@ -61,6 +89,11 @@ while let Some(y) = x.pop() {
61
89
62
90
## Iterator loops
63
91
92
+ > ** <sup >Syntax</sup >**
93
+ > _ IteratorLoopExpression_ :
94
+ >   ;  ; ` for ` _ Pattern_ ` in ` [ _ Expression_ ] <sub >except struct expression</sub >
95
+ > [ _ BlockExpression_ ]
96
+
64
97
A ` for ` expression is a syntactic construct for looping over elements provided
65
98
by an implementation of ` std::iter::IntoIterator ` . If the iterator yields a
66
99
value, that value is given the specified name and the body of the loop is
@@ -89,6 +122,10 @@ assert_eq!(sum, 55);
89
122
90
123
## Loop labels
91
124
125
+ > ** <sup >Syntax</sup >**
126
+ > _ LoopLabel_ :
127
+ >   ;  ; [ LIFETIME_OR_LABEL] ` : `
128
+
92
129
A loop expression may optionally have a _ label_ . The label is written as
93
130
a lifetime preceding the loop expression, as in ` 'foo: loop { break 'foo; } ` ,
94
131
` 'bar: while false {} ` , ` 'humbug: for _ in 0..0 {} ` .
@@ -99,6 +136,10 @@ expressions](#continue-expressions).
99
136
100
137
## ` break ` expressions
101
138
139
+ > ** <sup >Syntax</sup >**
140
+ > _ BreakExpression_ :
141
+ >   ;  ; ` break ` [ LIFETIME_OR_LABEL] <sup >?</sup > [ _ Expression_ ] <sup >?</sup >
142
+
102
143
When ` break ` is encountered, execution of the associated loop body is
103
144
immediately terminated, for example:
104
145
@@ -131,6 +172,10 @@ the forms `break`, `break 'label` or ([see below](#break-and-loop-values))
131
172
132
173
## ` continue ` expressions
133
174
175
+ > ** <sup >Syntax</sup >**
176
+ > _ ContinueExpression_ :
177
+ >   ;  ; ` continue ` [ LIFETIME_OR_LABEL] <sup >?</sup >
178
+
134
179
When ` continue ` is encountered, the current iteration of the associated loop
135
180
body is immediately terminated, returning control to the loop * head* . In
136
181
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,
165
210
and the ` loop ` must have a type compatible with each ` break ` expression.
166
211
` break ` without an expression is considered identical to ` break ` with
167
212
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