]>
Commit | Line | Data |
---|---|---|
ba9703b0 | 1 | The matched value was assigned in a match guard. |
60c5eb7d | 2 | |
ba9703b0 | 3 | Erroneous code example: |
60c5eb7d XL |
4 | |
5 | ```compile_fail,E0510 | |
6 | let mut x = Some(0); | |
7 | match x { | |
ba9703b0 XL |
8 | None => {} |
9 | Some(_) if { x = None; false } => {} // error! | |
10 | Some(_) => {} | |
60c5eb7d XL |
11 | } |
12 | ``` | |
13 | ||
ba9703b0 XL |
14 | When matching on a variable it cannot be mutated in the match guards, as this |
15 | could cause the match to be non-exhaustive. | |
16 | ||
60c5eb7d | 17 | Here executing `x = None` would modify the value being matched and require us |
ba9703b0 XL |
18 | to go "back in time" to the `None` arm. To fix it, change the value in the match |
19 | arm: | |
20 | ||
21 | ``` | |
22 | let mut x = Some(0); | |
23 | match x { | |
24 | None => {} | |
25 | Some(_) => { | |
26 | x = None; // ok! | |
27 | } | |
28 | } | |
29 | ``` |