@@ -157,81 +157,6 @@ match x {
157
157
See also the error E0303.
158
158
"## ,
159
159
160
- E0008 : r##"
161
- Names bound in match arms retain their type in pattern guards. As such, if a
162
- name is bound by move in a pattern, it should also be moved to wherever it is
163
- referenced in the pattern guard code. Doing so however would prevent the name
164
- from being available in the body of the match arm. Consider the following:
165
-
166
- ```compile_fail,E0008
167
- match Some("hi".to_string()) {
168
- Some(s) if s.len() == 0 => {}, // use s.
169
- _ => {},
170
- }
171
- ```
172
-
173
- The variable `s` has type `String`, and its use in the guard is as a variable of
174
- type `String`. The guard code effectively executes in a separate scope to the
175
- body of the arm, so the value would be moved into this anonymous scope and
176
- therefore becomes unavailable in the body of the arm.
177
-
178
- The problem above can be solved by using the `ref` keyword.
179
-
180
- ```
181
- match Some("hi".to_string()) {
182
- Some(ref s) if s.len() == 0 => {},
183
- _ => {},
184
- }
185
- ```
186
-
187
- Though this example seems innocuous and easy to solve, the problem becomes clear
188
- when it encounters functions which consume the value:
189
-
190
- ```compile_fail,E0008
191
- struct A{}
192
-
193
- impl A {
194
- fn consume(self) -> usize {
195
- 0
196
- }
197
- }
198
-
199
- fn main() {
200
- let a = Some(A{});
201
- match a {
202
- Some(y) if y.consume() > 0 => {}
203
- _ => {}
204
- }
205
- }
206
- ```
207
-
208
- In this situation, even the `ref` keyword cannot solve it, since borrowed
209
- content cannot be moved. This problem cannot be solved generally. If the value
210
- can be cloned, here is a not-so-specific solution:
211
-
212
- ```
213
- #[derive(Clone)]
214
- struct A{}
215
-
216
- impl A {
217
- fn consume(self) -> usize {
218
- 0
219
- }
220
- }
221
-
222
- fn main() {
223
- let a = Some(A{});
224
- match a{
225
- Some(ref y) if y.clone().consume() > 0 => {}
226
- _ => {}
227
- }
228
- }
229
- ```
230
-
231
- If the value will be consumed in the pattern guard, using its clone will not
232
- move its ownership, so the code works.
233
- "## ,
234
-
235
160
E0009 : r##"
236
161
In a pattern, all values that don't implement the `Copy` trait have to be bound
237
162
the same way. The goal here is to avoid binding simultaneously by-move and
@@ -475,13 +400,15 @@ for item in xs {
475
400
"## ,
476
401
477
402
E0301 : r##"
403
+ #### Note: this error code is no longer emitted by the compiler.
404
+
478
405
Mutable borrows are not allowed in pattern guards, because matching cannot have
479
406
side effects. Side effects could alter the matched object or the environment
480
407
on which the match depends in such a way, that the match would not be
481
408
exhaustive. For instance, the following would not match any arm if mutable
482
409
borrows were allowed:
483
410
484
- ```compile_fail,E0301
411
+ ```compile_fail,E0596
485
412
match Some(()) {
486
413
None => { },
487
414
option if option.take().is_none() => {
@@ -493,13 +420,15 @@ match Some(()) {
493
420
"## ,
494
421
495
422
E0302 : r##"
423
+ #### Note: this error code is no longer emitted by the compiler.
424
+
496
425
Assignments are not allowed in pattern guards, because matching cannot have
497
426
side effects. Side effects could alter the matched object or the environment
498
427
on which the match depends in such a way, that the match would not be
499
428
exhaustive. For instance, the following would not match any arm if assignments
500
429
were allowed:
501
430
502
- ```compile_fail,E0302
431
+ ```compile_fail,E0594
503
432
match Some(()) {
504
433
None => { },
505
434
option if { option = None; false } => { },
0 commit comments