struct Point {
x: f64,
y: f64,
}
enum Shape {
Circle(Point, f64),
Rectangle(Point, Point),
}
let my_shape = Shape::Circle(Point { x: 0.0, y: 0.0 },
Here is another way to do it:
struct Point {
x: f64,
y: f64,
}
enum Shape {
Circle(Point, f64),
}
fn main() {
let Shape::Circle(_, radius) = Shape::Circle(Point { x: 0.0, y: 0.0 }, 10.0);
println!("value: {}", radius);
}
This only works if the pattern is irrefutable, such as when the enum type you're matching on only has one variant. To make this work, I had to remove the unused Rectangle
variant.
In cases where you have more than one variant, you'll probably want the full match expression anyway, since you're presumably handling more than just one kind of shape.