@@ -75,11 +75,13 @@ the following is invalid as it requires the entire Option<String> to be moved
75
75
into a variable called `op_string` while simultaneously requiring the inner
76
76
String to be moved into a variable called `s`.
77
77
78
+ ```
78
79
let x = Some("s".to_string());
79
80
match x {
80
81
op_string @ Some(s) => ...
81
82
None => ...
82
83
}
84
+ ```
83
85
84
86
See also Error 303.
85
87
"## ,
@@ -90,10 +92,12 @@ name is bound by move in a pattern, it should also be moved to wherever it is
90
92
referenced in the pattern guard code. Doing so however would prevent the name
91
93
from being available in the body of the match arm. Consider the following:
92
94
95
+ ```
93
96
match Some("hi".to_string()) {
94
97
Some(s) if s.len() == 0 => // use s.
95
98
...
96
99
}
100
+ ```
97
101
98
102
The variable `s` has type String, and its use in the guard is as a variable of
99
103
type String. The guard code effectively executes in a separate scope to the body
@@ -102,11 +106,13 @@ become unavailable in the body of the arm. Although this example seems
102
106
innocuous, the problem is most clear when considering functions that take their
103
107
argument by value.
104
108
109
+ ```
105
110
match Some("hi".to_string()) {
106
111
Some(s) if { drop(s); false } => (),
107
112
Some(s) => // use s.
108
113
...
109
114
}
115
+ ```
110
116
111
117
The value would be dropped in the guard then become unavailable not only in the
112
118
body of that arm but also in all subsequent arms! The solution is to bind by
@@ -218,6 +224,7 @@ An if-let pattern attempts to match the pattern, and enters the body if the
218
224
match was succesful. If the match is irrefutable (when it cannot fail to match),
219
225
use a regular `let`-binding instead. For instance:
220
226
227
+ ```
221
228
struct Irrefutable(i32);
222
229
let irr = Irrefutable(0);
223
230
@@ -230,13 +237,15 @@ if let Irrefutable(x) = irr {
230
237
// Try this instead:
231
238
let Irrefutable(x) = irr;
232
239
foo(x);
240
+ ```
233
241
"## ,
234
242
235
243
E0165 : r##"
236
244
A while-let pattern attempts to match the pattern, and enters the body if the
237
245
match was succesful. If the match is irrefutable (when it cannot fail to match),
238
246
use a regular `let`-binding inside a `loop` instead. For instance:
239
247
248
+ ```
240
249
struct Irrefutable(i32);
241
250
let irr = Irrefutable(0);
242
251
@@ -250,6 +259,7 @@ loop {
250
259
let Irrefutable(x) = irr;
251
260
...
252
261
}
262
+ ```
253
263
"## ,
254
264
255
265
E0170 : r##"
@@ -304,6 +314,7 @@ that a name will be extracted in all cases. Instead of pattern matching the
304
314
loop variable, consider using a `match` or `if let` inside the loop body. For
305
315
instance:
306
316
317
+ ```
307
318
// This fails because `None` is not covered.
308
319
for Some(x) in xs {
309
320
...
@@ -323,6 +334,7 @@ for item in xs {
323
334
...
324
335
}
325
336
}
337
+ ```
326
338
"## ,
327
339
328
340
E0301 : r##"
@@ -332,11 +344,13 @@ on which the match depends in such a way, that the match would not be
332
344
exhaustive. For instance, the following would not match any arm if mutable
333
345
borrows were allowed:
334
346
347
+ ```
335
348
match Some(()) {
336
349
None => { },
337
350
option if option.take().is_none() => { /* impossible, option is `Some` */ },
338
351
Some(_) => { } // When the previous match failed, the option became `None`.
339
352
}
353
+ ```
340
354
"## ,
341
355
342
356
E0302 : r##"
@@ -346,21 +360,24 @@ on which the match depends in such a way, that the match would not be
346
360
exhaustive. For instance, the following would not match any arm if assignments
347
361
were allowed:
348
362
363
+ ```
349
364
match Some(()) {
350
365
None => { },
351
366
option if { option = None; false } { },
352
367
Some(_) => { } // When the previous match failed, the option became `None`.
353
368
}
369
+ ```
354
370
"## ,
355
371
356
372
E0303 : r##"
357
373
In certain cases it is possible for sub-bindings to violate memory safety.
358
374
Updates to the borrow checker in a future version of Rust may remove this
359
375
restriction, but for now patterns must be rewritten without sub-bindings.
360
376
361
- // Before.
362
- match Some("hi".to_string()) {
363
- ref op_string_ref @ Some(ref s) => ...
377
+ ```
378
+ // Code like this...
379
+ match Some(5) {
380
+ ref op_num @ Some(num) => ...
364
381
None => ...
365
382
}
366
383
@@ -372,6 +389,7 @@ match Some("hi".to_string()) {
372
389
}
373
390
None => ...
374
391
}
392
+ ```
375
393
376
394
The `op_string_ref` binding has type &Option<&String> in both cases.
377
395
0 commit comments