nandi/rustnimpublic Fork 0
ae9f986fc43d0fc9ab029946e2c7a25cdc72b4ed
Commits
Clone
git clone https://git.rickub.com/nandi/rustnim.git
git clone ssh://git@rickub.com/nandi/rustnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

021-option.rs · 25 lines · 638 BRust Blame HistoryRaw
Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 18h ago1// Option, and the type alias expansion that `Result<T>` in base16ct needs.
2type MyResult<T> = Result<T, i32>;
3
4fn 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
14fn 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
20fn 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}