| 1 |
enum Res { |
| 2 |
OK, |
| 3 |
BAD, |
| 4 |
} |
| 5 |
|
| 6 |
enum LOption { |
| 7 |
Some(i32), |
| 8 |
None, |
| 9 |
} |
| 10 |
|
| 11 |
// Expect a Some(_) |
| 12 |
// |
| 13 |
// Check we can match a Some. |
| 14 |
fn test_can_destructure_Some(v: LOption) -> Res { |
| 15 |
if let LOption::Some(v) = v { |
| 16 |
return Res::OK; |
| 17 |
} |
| 18 |
return Res::BAD; |
| 19 |
} |
| 20 |
|
| 21 |
// Expect Some(100). |
| 22 |
// |
| 23 |
// Check we can destructure and the inner value is correct. |
| 24 |
fn test_inner_value_is_100(v: LOption) -> Res { |
| 25 |
if let LOption::Some(v) = v { |
| 26 |
return match v { |
| 27 |
100 => Res::OK, |
| 28 |
_ => Res::BAD, |
| 29 |
} |
| 30 |
} |
| 31 |
return Res::BAD; |
| 32 |
} |
| 33 |
|
| 34 |
// Expect a None as actual parameter. |
| 35 |
// |
| 36 |
// Only when we FAIL to match a Some do we take the else and return OK. |
| 37 |
fn test_if_else(v: LOption) -> Res { |
| 38 |
if let LOption::Some(v) = v { |
| 39 |
return Res::BAD; |
| 40 |
} else { |
| 41 |
return Res::OK; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
fn main() -> i32 { |
| 46 |
|
| 47 |
// Passing a None, so the function should return BAD |
| 48 |
match test_can_destructure_Some(LOption::None) { |
| 49 |
Res::OK => return 1, |
| 50 |
Res::BAD => (), |
| 51 |
} |
| 52 |
|
| 53 |
// Same, but with a Some, should return OK |
| 54 |
match test_can_destructure_Some(LOption::Some(1)) { |
| 55 |
Res::OK => (), |
| 56 |
Res::BAD => return 1, |
| 57 |
} |
| 58 |
|
| 59 |
// Check the destructuring is correct by looking for Some(100) |
| 60 |
match test_inner_value_is_100(LOption::Some(100)) { |
| 61 |
Res::OK => (), |
| 62 |
Res::BAD => return 1, |
| 63 |
} |
| 64 |
|
| 65 |
// ... passing Some(1) should return BAD |
| 66 |
match test_inner_value_is_100(LOption::Some(1)) { |
| 67 |
Res::OK => return 1, |
| 68 |
Res::BAD => (), |
| 69 |
} |
| 70 |
|
| 71 |
// ... and so does passing None |
| 72 |
match test_inner_value_is_100(LOption::None) { |
| 73 |
Res::OK => return 1, |
| 74 |
Res::BAD => (), |
| 75 |
} |
| 76 |
|
| 77 |
// Check if let... else ... |
| 78 |
match test_if_else(LOption::None) { |
| 79 |
Res::OK => (), |
| 80 |
Res::BAD => return 1, |
| 81 |
} |
| 82 |
|
| 83 |
0 |
| 84 |
} |