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);
}
|