I’m playing with the following code and can’t seem to find an example where I can get the values of one of the coordinates.

enum Coordinates {
    Point1 { x: i32, y: i32 },
    Point2 { x: i32, y: i32 },
}
fn main() {
    let p1 = Coordinates::Point1{ x: 0, y: 45};
    let x = p1.x; //Doesn't work
}

How can I get the value of x or y?

  • Ephera@lemmy.ml
    link
    fedilink
    arrow-up
    1
    ·
    3 years ago

    Hmm, yeah, this is good. Coming from more object-oriented programming languages, my first intuition was to throw down a trait with those shared methods/fields, which then both coordinate-types implement, but that’s rather clunky in Rust, since traits cannot define fields…