| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 1 | // 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)] |
| 4 | enum Shape { |
| 5 | Empty, |
| 6 | Square(i32), |
| 7 | Rect { w: i32, h: i32 }, |
| 8 | } |
| 9 | |
| 10 | fn 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 | |
| 18 | fn 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 | } |