E.g. in the code below, ThreeD is an enum which can either be a Point or a Vector. The function cross is intended to calculate the cross product of two Vectors, and panic if either argument is a Point.
In order to calculate the result I need the x, y and z components of both the lhs and the rhs. As it stands however the second match will obscure the x,y and z members from the first match.
One option is to explicitly assign the x, y, and z members to temporary variables, immediately after the first let statement, but this feels horribly klunky.
pub fn cross(lhs: ThreeD, rhs: ThreeD) -> ThreeD {
if let Vector { x, y, z } = lhs {
let lhs_x = x; // I DON'T LIKE THIS APPROACH!
let lhs_y = y; //
let lhs_z = z; //
if let Vector { x, y, z } = rhs {
// dummy calc here - I know this is wrong ;-)
Vector { x: lhs_x * x, y: lhs_y * y, z: lhs_z * z }
} else {
panic!("Cannot calculate cross product with a point")
}
} else {
panic!("Cannot calculate cross product against a point")
}
}
>Solution :
You can bind to a different name within the pattern using this syntax:
if let Vector { x: lhs_x, y: lhs_y, z: lhs_z } = lhs {