| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 18h ago | 1 | // Option, and the type alias expansion that `Result<T>` in base16ct needs. |
| 2 | type MyResult<T> = Result<T, i32>; |
| 3 | |
| 4 | fn first_even(a: i32, b: i32) -> Option<i32> { |
| 5 | if a % 2 == 0 { |
| 6 | Some(a) |
| 7 | } else if b % 2 == 0 { |
| 8 | Some(b) |
| 9 | } else { |
| 10 | None |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | fn checked(n: i32) -> MyResult<i32> { |
| 15 | let v: Option<i32> = first_even(n, n + 1); |
| 16 | let got: i32 = v.ok_or(-1)?; |
| 17 | Ok(got * 10) |
| 18 | } |
| 19 | |
| 20 | fn main() { |
| 21 | println!("{:?} {:?}", first_even(2, 3), first_even(1, 3)); |
| 22 | println!("{}", first_even(1, 4).unwrap()); |
| 23 | println!("{}", first_even(1, 3).unwrap_or(-7)); |
| 24 | println!("{:?} {:?}", checked(2), checked(1)); |
| 25 | } |