Ömer Şengül

Enum & Match

byÖmer ŞengülCreated January 26, 2024 at 07:00 PM
Enum & Match
1enum Shape {
2  Circle(f64),
3  Rectangle(f64, f64),
4}
5
6fn area(shape: Shape) -> f64 {
7  match shape {
8    Shape::Circle(r) => std::f64::consts::PI * r * r,
9    Shape::Rectangle(w, h) => w * h,
10  }
11}

Description

Rich enums with data and exhaustive pattern matching.

Discussion (0)

Sort by: