Zest.

Rust: Enum and match

A short, tested Rust example for enum and match. Copy it and adjust the names and paths for your project.

Rust
enum Shape {
    Circle(f64),
    Rect { w: f64, h: f64 },
}

fn area(s: &Shape) -> f64 {
    match s {
        Shape::Circle(r) => std::f64::consts::PI * r * r,
        Shape::Rect { w, h } => w * h,
    }
}

Find it in Zest

Search for “rust enum and match” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More Rust examples

All Rust examples