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
018-enum-payload.rs · 24 lines · 618 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
// A data-carrying enum becomes a Nim object variant, and a pattern that binds
// becomes an if/elif chain, because Nim's `case` cannot destructure.
#[derive(Debug)]
enum Shape {
    Empty,
    Square(i32),
    Rect { w: i32, h: i32 },
}

fn area(s: Shape) -> i32 {
    match s {
        Shape::Empty => 0,
        Shape::Square(a) => a * a,
        Shape::Rect { w, h } => w * h,
    }
}

fn main() {
    println!("{}", area(Shape::Empty));
    println!("{}", area(Shape::Square(4)));
    println!("{}", area(Shape::Rect { w: 3, h: 5 }));
    println!("{:?}", Shape::Square(4));
    println!("{:?}", Shape::Empty);
}