0

Given the following code

enum shape {
  Circle,
  Square,
  Rectangle
}

struct A {
  b: shape,
  c: String
}

struct Plan {
 items : Vec<A>
}

impl Plan {

fn draw_shapes() {
    self.items.iter().find_map(|d| match ??? { // How to match on a field of struct?
            shapes::Circle => draw_circle(d),
            shapes::Square => draw_square(d),
            shapes::Rectanble => draw_rectangle(d),
        })
    }
}
}

I want to match a field of struct A and call the respective draw functions.

A. K.
  • 34,395
  • 15
  • 52
  • 89
  • 4
    What about `match d.b`? – Chayim Friedman May 01 '23 at 14:17
  • You also don't need the wildcard. – Chayim Friedman May 01 '23 at 14:18
  • SO is not a forum – Stargateur May 01 '23 at 14:22
  • 1
    @Stargateur How is that related here? – Chayim Friedman May 01 '23 at 14:23
  • @ChayimFriedman It's not mean to answer every basic question like that, first there is countless of similar question on SO, OP just didn't show any search effort, no code formation, unclear and didn't even read the rust book. This question look more like a forum question, a reddit question or discord question fine ask on these platform such bad question but I don't want this in SO. – Stargateur May 01 '23 at 14:24
  • 1
    You should probably call that enum `Shape`. – tadman May 01 '23 at 14:24
  • 2
    @Stargateur As long as this question was not already asked, it is suitable for SO (even if worth downvotes) as basic as it may be, and I couldn't find any duplicate, not even "How do I access fields in Rust". – Chayim Friedman May 01 '23 at 14:28
  • @Stargateur This question is fine. I think you're over-reacting here. There are far worse questions to pick on. – tadman May 01 '23 at 14:38
  • https://stackoverflow.com/questions/9109872/how-do-you-access-enum-values-in-rust/33723601#33723601 https://stackoverflow.com/questions/40961938/referring-to-matched-value-in-rust/40962017#40962017 https://stackoverflow.com/questions/41390457/how-to-match-struct-fields-in-rust/41394755#41394755 https://stackoverflow.com/questions/51429501/how-do-i-conditionally-check-if-an-enum-is-one-variant-or-another/51429606#51429606 @ChayimFriedman to name a few – Stargateur May 01 '23 at 14:38
  • @Stargateur I consider none of these a duplicate, but you can disagree. – Chayim Friedman May 01 '23 at 14:40
  • 1
    @ChayimFriedman `match d.b` worked! If you want to put that as answer I'll accept it. Not sure why @Stargateur thinks this is a duplicate. – A. K. May 01 '23 at 15:03

1 Answers1

4

You simply need to match over the property access on the struct, in this case you would be using match d.b.

    self.items.iter().find_map(|d| match d.b { // How to match on a field of struct?
            shapes::Circle => draw_circle(d),
            shapes::Square => draw_square(d),
            shapes::Rectanble => draw_rectangle(d),
        })
    }

This is because match blocks match over any expression, not just an identifier.

0xLogN
  • 3,289
  • 1
  • 14
  • 35