@@ -75,11 +75,13 @@ the following is invalid as it requires the entire Option<String> to be moved
7575into a variable called `op_string` while simultaneously requiring the inner 
7676String to be moved into a variable called `s`. 
7777
78+ ``` 
7879let x = Some("s".to_string()); 
7980match x { 
8081    op_string @ Some(s) => ... 
8182    None => ... 
8283} 
84+ ``` 
8385
8486See also Error 303. 
8587"## , 
@@ -90,10 +92,12 @@ name is bound by move in a pattern, it should also be moved to wherever it is
9092referenced in the pattern guard code. Doing so however would prevent the name 
9193from being available in the body of the match arm. Consider the following: 
9294
95+ ``` 
9396match Some("hi".to_string()) { 
9497    Some(s) if s.len() == 0 => // use s. 
9598    ... 
9699} 
100+ ``` 
97101
98102The variable `s` has type String, and its use in the guard is as a variable of 
99103type 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
102106innocuous, the problem is most clear when considering functions that take their 
103107argument by value. 
104108
109+ ``` 
105110match Some("hi".to_string()) { 
106111    Some(s) if { drop(s); false } => (), 
107112    Some(s) => // use s. 
108113    ... 
109114} 
115+ ``` 
110116
111117The value would be dropped in the guard then become unavailable not only in the 
112118body of that arm but also in all subsequent arms! The solution is to bind by 
@@ -219,8 +225,10 @@ them yourself.
219225You can build a free-standing crate by adding `#![no_std]` to the crate 
220226attributes: 
221227
228+ ``` 
222229#![feature(no_std)] 
223230#![no_std] 
231+ ``` 
224232
225233See also https://doc.rust-lang.org/book/no-stdlib.html 
226234"## , 
@@ -236,11 +244,13 @@ mutex can be declared `static` as well.
236244
237245If you want to match against a `static`, consider using a guard instead: 
238246
247+ ``` 
239248static FORTY_TWO: i32 = 42; 
240249match Some(42) { 
241250    Some(x) if x == FORTY_TWO => ... 
242251    ... 
243252} 
253+ ``` 
244254"## , 
245255
246256E0161 :  r##" 
@@ -256,6 +266,7 @@ An if-let pattern attempts to match the pattern, and enters the body if the
256266match was succesful. If the match is irrefutable (when it cannot fail to match), 
257267use a regular `let`-binding instead. For instance: 
258268
269+ ``` 
259270struct Irrefutable(i32); 
260271let irr = Irrefutable(0); 
261272
@@ -268,13 +279,15 @@ if let Irrefutable(x) = irr {
268279// Try this instead: 
269280let Irrefutable(x) = irr; 
270281foo(x); 
282+ ``` 
271283"## , 
272284
273285E0165 :  r##" 
274286A while-let pattern attempts to match the pattern, and enters the body if the 
275287match was succesful. If the match is irrefutable (when it cannot fail to match), 
276288use a regular `let`-binding inside a `loop` instead. For instance: 
277289
290+ ``` 
278291struct Irrefutable(i32); 
279292let irr = Irrefutable(0); 
280293
@@ -288,22 +301,27 @@ loop {
288301    let Irrefutable(x) = irr; 
289302    ... 
290303} 
304+ ``` 
291305"## , 
292306
293307E0170 :  r##" 
294308Enum variants are qualified by default. For example, given this type: 
295309
310+ ``` 
296311enum Method { 
297312    GET, 
298313    POST 
299314} 
315+ ``` 
300316
301317you would match it using: 
302318
319+ ``` 
303320match m { 
304321    Method::GET => ... 
305322    Method::POST => ... 
306323} 
324+ ``` 
307325
308326If you don't qualify the names, the code will bind new variables named "GET" and 
309327"POST" instead. This behavior is likely not what you want, so rustc warns when 
@@ -312,8 +330,10 @@ that happens.
312330Qualified names are good practice, and most code works well with them. But if 
313331you prefer them unqualified, you can import the variants into scope: 
314332
333+ ``` 
315334use Method::*; 
316335enum Method { GET, POST } 
336+ ``` 
317337"## , 
318338
319339E0267 :  r##" 
@@ -333,7 +353,9 @@ E0296: r##"
333353This error indicates that the given recursion limit could not be parsed. Ensure 
334354that the value provided is a positive integer between quotes, like so: 
335355
356+ ``` 
336357#![recursion_limit="1000"] 
358+ ``` 
337359"## , 
338360
339361E0297 :  r##" 
@@ -342,6 +364,7 @@ that a name will be extracted in all cases. Instead of pattern matching the
342364loop variable, consider using a `match` or `if let` inside the loop body. For 
343365instance: 
344366
367+ ``` 
345368// This fails because `None` is not covered. 
346369for Some(x) in xs { 
347370    ... 
@@ -361,6 +384,7 @@ for item in xs {
361384        ... 
362385    } 
363386} 
387+ ``` 
364388"## , 
365389
366390E0301 :  r##" 
@@ -370,11 +394,13 @@ on which the match depends in such a way, that the match would not be
370394exhaustive. For instance, the following would not match any arm if mutable 
371395borrows were allowed: 
372396
397+ ``` 
373398match Some(()) { 
374399    None => { }, 
375400    option if option.take().is_none() => { /* impossible, option is `Some` */ }, 
376401    Some(_) => { } // When the previous match failed, the option became `None`. 
377402} 
403+ ``` 
378404"## , 
379405
380406E0302 :  r##" 
@@ -384,21 +410,24 @@ on which the match depends in such a way, that the match would not be
384410exhaustive. For instance, the following would not match any arm if assignments 
385411were allowed: 
386412
413+ ``` 
387414match Some(()) { 
388415    None => { }, 
389416    option if { option = None; false } { }, 
390417    Some(_) => { } // When the previous match failed, the option became `None`. 
391418} 
419+ ``` 
392420"## , 
393421
394422E0303 :  r##" 
395423In certain cases it is possible for sub-bindings to violate memory safety. 
396424Updates to the borrow checker in a future version of Rust may remove this 
397425restriction, but for now patterns must be rewritten without sub-bindings. 
398426
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) => ... 
402431    None => ... 
403432} 
404433
@@ -410,6 +439,7 @@ match Some("hi".to_string()) {
410439    } 
411440    None => ... 
412441} 
442+ ``` 
413443
414444The `op_string_ref` binding has type &Option<&String> in both cases. 
415445
0 commit comments