this post was submitted on 12 Mar 2022
3 points (100.0% liked)

Rust Programming

8346 readers
15 users here now

founded 5 years ago
MODERATORS
 

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?

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 2 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...