]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0408.md
Rollup merge of #62514 - stephaneyfx:box-ffi, r=nikomatsakis
[rust.git] / src / librustc_error_codes / error_codes / E0408.md
1 An "or" pattern was used where the variable bindings are not consistently bound
2 across patterns.
3
4 Erroneous code example:
5
6 ```compile_fail,E0408
7 match x {
8     Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is
9                                       //        not bound in pattern #2
10     _ => ()
11 }
12 ```
13
14 Here, `y` is bound to the contents of the `Some` and can be used within the
15 block corresponding to the match arm. However, in case `x` is `None`, we have
16 not specified what `y` is, and the block will use a nonexistent variable.
17
18 To fix this error, either split into multiple match arms:
19
20 ```
21 let x = Some(1);
22 match x {
23     Some(y) => { /* use y */ }
24     None => { /* ... */ }
25 }
26 ```
27
28 or, bind the variable to a field of the same type in all sub-patterns of the
29 or pattern:
30
31 ```
32 let x = (0, 2);
33 match x {
34     (0, y) | (y, 0) => { /* use y */}
35     _ => {}
36 }
37 ```
38
39 In this example, if `x` matches the pattern `(0, _)`, the second field is set
40 to `y`. If it matches `(_, 0)`, the first field is set to `y`; so in all
41 cases `y` is set to some value.