File tree 2 files changed +71
-0
lines changed
2 files changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -92,5 +92,24 @@ let a = if let Some(1) = x {
92
92
assert_eq! (a , 3 );
93
93
```
94
94
95
+ An ` if let ` expression is equivalent to a ` match ` expression as follows:
96
+
97
+ ``` rust
98
+ if let PAT = EXPR {
99
+ /* body */
100
+ } else {
101
+ /*else */
102
+ }
103
+ ```
104
+
105
+ is equivalent to
106
+
107
+ ``` rust
108
+ match EXPR {
109
+ PAT => { /* body */ },
110
+ _ => { /* else */ }, // () if there is no else
111
+ }
112
+ ```
113
+
95
114
[ _Expression_ ] : expressions.html
96
115
[ _BlockExpression_ ] : expressions/block-expr.html
Original file line number Diff line number Diff line change @@ -85,6 +85,26 @@ while let Some(y) = x.pop() {
85
85
}
86
86
```
87
87
88
+ A ` while let ` loop is equivalent to a ` loop ` expression containing a ` match `
89
+ expression as follows.
90
+
91
+ ``` rust
92
+ 'label : while let PAT = EXPR {
93
+ /* loop body */
94
+ }
95
+ ```
96
+
97
+ is equivalent to
98
+
99
+ ``` rust
100
+ 'label : loop {
101
+ match EXPR {
102
+ PAT => { /* loop body */ },
103
+ _ => break ,
104
+ }
105
+ }
106
+ ```
107
+
88
108
## Iterator loops
89
109
90
110
> ** <sup >Syntax</sup >**
@@ -118,6 +138,37 @@ for n in 1..11 {
118
138
assert_eq! (sum , 55 );
119
139
```
120
140
141
+ A for loop is equivalent to the following block expression.
142
+
143
+ ``` rust
144
+ 'label : for PATTERN in iter_expr {
145
+ /* loop body */
146
+ }
147
+ ```
148
+
149
+ is equivalent to
150
+
151
+ ``` rust
152
+ {
153
+ let result = match IntoIterator :: into_iter (iter_expr ) {
154
+ mut iter => 'label : loop {
155
+ let next ;
156
+ match iter . next () {
157
+ Some (val ) => next = val ,
158
+ None => break ,
159
+ };
160
+ let PAT = next ;
161
+ let () = { /* loop body */ };
162
+ },
163
+ };
164
+ result
165
+ }
166
+ ```
167
+
168
+ > ** Note** : that the outer ` match ` is used to ensure that any
169
+ > [ temporary values] in ` iter_expr ` don't get dropped before the loop is
170
+ > finished.
171
+
121
172
## Loop labels
122
173
123
174
> ** <sup >Syntax</sup >**
@@ -210,6 +261,7 @@ and the `loop` must have a type compatible with each `break` expression.
210
261
expression ` () ` .
211
262
212
263
[ IDENTIFIER ] : identifiers.html
264
+ [ temporary values ] : expressions.html#temporary-lifetimes
213
265
214
266
[ _Expression_ ] : expressions.html
215
267
[ _BlockExpression_ ] : expressions/block-expr.html
You can’t perform that action at this time.
0 commit comments