nandi/rustnimpublic Fork 0
afb2a6e152356d5a78d536ad75f005bb1fc7ed63
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.

Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 · on afb2a6e152356d5a78d536ad75f005bb1fc7ed63 · nandithebull · 18h ago
019-result.rs · 25 lines · 558 BRust Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Result-returning functions, Ok/Err construction, and matching on both.
#[derive(Debug, PartialEq)]
enum Error {
    TooBig,
}

fn halve(n: i32) -> Result<i32, Error> {
    if n > 100 {
        Err(Error::TooBig)
    } else {
        Ok(n / 2)
    }
}

fn main() {
    for n in [10, 200] {
        match halve(n) {
            Ok(v) => println!("ok {}", v),
            Err(e) => println!("err {:?}", e),
        }
    }
    println!("{:?}", halve(8));
    println!("{}", halve(8).unwrap());
    println!("{} {}", halve(8).is_ok(), halve(500).is_err());
}