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

018-enum-payload.rs · 24 lines · 618 BRust Blame HistoryRaw
Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 20h ago1// A data-carrying enum becomes a Nim object variant, and a pattern that binds
2// becomes an if/elif chain, because Nim's `case` cannot destructure.
3#[derive(Debug)]
4enum Shape {
5 Empty,
6 Square(i32),
7 Rect { w: i32, h: i32 },
8}
9
10fn area(s: Shape) -> i32 {
11 match s {
12 Shape::Empty => 0,
13 Shape::Square(a) => a * a,
14 Shape::Rect { w, h } => w * h,
15 }
16}
17
18fn main() {
19 println!("{}", area(Shape::Empty));
20 println!("{}", area(Shape::Square(4)));
21 println!("{}", area(Shape::Rect { w: 3, h: 5 }));
22 println!("{:?}", Shape::Square(4));
23 println!("{:?}", Shape::Empty);
24}