Fundamentals22 / 42
Up next

Enums with data

enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
}

fn area(s: &Shape) -> f64 {
    match s {
        Shape::Circle(r) => 3.14159 * r * r,
        Shape::Rectangle(w, h) => w * h,
    }
}

fn main() {
    println!("{:.2}", area(&Shape::Rectangle(3.0, 4.0)));
}
What you're solving

variants can carry values; match them