Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way for me to implicitly bind the members of a let/match operation?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 {
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading