@@ -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
@@ -219,8 +225,10 @@ them yourself.
219
225
You can build a free-standing crate by adding `#![no_std]` to the crate
220
226
attributes:
221
227
228
+ ```
222
229
#![feature(no_std)]
223
230
#![no_std]
231
+ ```
224
232
225
233
See also https://doc.rust-lang.org/book/no-stdlib.html
226
234
"## ,
@@ -236,11 +244,13 @@ mutex can be declared `static` as well.
236
244
237
245
If you want to match against a `static`, consider using a guard instead:
238
246
247
+ ```
239
248
static FORTY_TWO: i32 = 42;
240
249
match Some(42) {
241
250
Some(x) if x == FORTY_TWO => ...
242
251
...
243
252
}
253
+ ```
244
254
"## ,
245
255
246
256
E0161 : r##"
@@ -256,6 +266,7 @@ An if-let pattern attempts to match the pattern, and enters the body if the
256
266
match was succesful. If the match is irrefutable (when it cannot fail to match),
257
267
use a regular `let`-binding instead. For instance:
258
268
269
+ ```
259
270
struct Irrefutable(i32);
260
271
let irr = Irrefutable(0);
261
272
@@ -268,13 +279,15 @@ if let Irrefutable(x) = irr {
268
279
// Try this instead:
269
280
let Irrefutable(x) = irr;
270
281
foo(x);
282
+ ```
271
283
"## ,
272
284
273
285
E0165 : r##"
274
286
A while-let pattern attempts to match the pattern, and enters the body if the
275
287
match was succesful. If the match is irrefutable (when it cannot fail to match),
276
288
use a regular `let`-binding inside a `loop` instead. For instance:
277
289
290
+ ```
278
291
struct Irrefutable(i32);
279
292
let irr = Irrefutable(0);
280
293
@@ -288,22 +301,27 @@ loop {
288
301
let Irrefutable(x) = irr;
289
302
...
290
303
}
304
+ ```
291
305
"## ,
292
306
293
307
E0170 : r##"
294
308
Enum variants are qualified by default. For example, given this type:
295
309
310
+ ```
296
311
enum Method {
297
312
GET,
298
313
POST
299
314
}
315
+ ```
300
316
301
317
you would match it using:
302
318
319
+ ```
303
320
match m {
304
321
Method::GET => ...
305
322
Method::POST => ...
306
323
}
324
+ ```
307
325
308
326
If you don't qualify the names, the code will bind new variables named "GET" and
309
327
"POST" instead. This behavior is likely not what you want, so rustc warns when
@@ -312,8 +330,10 @@ that happens.
312
330
Qualified names are good practice, and most code works well with them. But if
313
331
you prefer them unqualified, you can import the variants into scope:
314
332
333
+ ```
315
334
use Method::*;
316
335
enum Method { GET, POST }
336
+ ```
317
337
"## ,
318
338
319
339
E0267 : r##"
@@ -333,7 +353,9 @@ E0296: r##"
333
353
This error indicates that the given recursion limit could not be parsed. Ensure
334
354
that the value provided is a positive integer between quotes, like so:
335
355
356
+ ```
336
357
#![recursion_limit="1000"]
358
+ ```
337
359
"## ,
338
360
339
361
E0297 : r##"
@@ -342,6 +364,7 @@ that a name will be extracted in all cases. Instead of pattern matching the
342
364
loop variable, consider using a `match` or `if let` inside the loop body. For
343
365
instance:
344
366
367
+ ```
345
368
// This fails because `None` is not covered.
346
369
for Some(x) in xs {
347
370
...
@@ -361,6 +384,7 @@ for item in xs {
361
384
...
362
385
}
363
386
}
387
+ ```
364
388
"## ,
365
389
366
390
E0301 : r##"
@@ -370,11 +394,13 @@ on which the match depends in such a way, that the match would not be
370
394
exhaustive. For instance, the following would not match any arm if mutable
371
395
borrows were allowed:
372
396
397
+ ```
373
398
match Some(()) {
374
399
None => { },
375
400
option if option.take().is_none() => { /* impossible, option is `Some` */ },
376
401
Some(_) => { } // When the previous match failed, the option became `None`.
377
402
}
403
+ ```
378
404
"## ,
379
405
380
406
E0302 : r##"
@@ -384,21 +410,24 @@ on which the match depends in such a way, that the match would not be
384
410
exhaustive. For instance, the following would not match any arm if assignments
385
411
were allowed:
386
412
413
+ ```
387
414
match Some(()) {
388
415
None => { },
389
416
option if { option = None; false } { },
390
417
Some(_) => { } // When the previous match failed, the option became `None`.
391
418
}
419
+ ```
392
420
"## ,
393
421
394
422
E0303 : r##"
395
423
In certain cases it is possible for sub-bindings to violate memory safety.
396
424
Updates to the borrow checker in a future version of Rust may remove this
397
425
restriction, but for now patterns must be rewritten without sub-bindings.
398
426
399
- // Before.
400
- match Some("hi".to_string()) {
401
- ref op_string_ref @ Some(ref s) => ...
427
+ ```
428
+ // Code like this...
429
+ match Some(5) {
430
+ ref op_num @ Some(num) => ...
402
431
None => ...
403
432
}
404
433
@@ -410,6 +439,7 @@ match Some("hi".to_string()) {
410
439
}
411
440
None => ...
412
441
}
442
+ ```
413
443
414
444
The `op_string_ref` binding has type &Option<&String> in both cases.
415
445
0 commit comments