nandi/rustnimpublic Fork 0
3d4a7d283b83d3a5305dfc6654c290bd4ee162ae
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 3d4a7d283b83d3a5305dfc6654c290bd4ee162ae · nandithebull · 11h ago
020-question-mark.rs · 24 lines · 437 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
// `?` expands to an early return on the error branch.
#[derive(Debug)]
enum Error {
    Odd,
}

fn halve(n: i32) -> Result<i32, Error> {
    if n % 2 != 0 {
        return Err(Error::Odd);
    }
    Ok(n / 2)
}

fn quarter(n: i32) -> Result<i32, Error> {
    let half = halve(n)?;
    let q = halve(half)?;
    Ok(q)
}

fn main() {
    println!("{:?}", quarter(8));
    println!("{:?}", quarter(6));
    println!("{:?}", quarter(5));
}