Rust Enums and match
A Rust enum lists the forms a value can take, and each form can carry data. Shape::Circle(u32) holds a radius and Shape::Square(u32) holds a side length.
Rust
enum Shape {
Circle(u32),
Square(u32),
}
fn area(shape: &Shape) -> u32 {
match shape {
Shape::Circle(r) => 3 * r * r,
Shape::Square(s) => s * s,
}
}
fn main() {
println!("{}", area(&Shape::Circle(2)));
println!("{}", area(&Shape::Square(5)));
}