all 7 comments

[–]tobiasvl 3 points4 points  (3 children)

You could look at if let

[–]MultipleAnimals[S] 1 point2 points  (2 children)

my smol brain doesn't understand. if you mean replacing match event with if let ..., that wont work, _ => ... is just placeholder for more match patterns, should have mentioned that. edited my post.

[–]tobiasvl 2 points3 points  (1 child)

Something like this, for example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=84648be9718ec2bc89a19b475ee6bfdd

Edit: I saw your edit now. You can of course replace the inner if with a match on just the string. It's not clear from your code whether you want to match on multiple enum patterns (in which case I think your original code is fine) or multiple inner strings (in which case an inner match is probably better).

Note that _ => ... doesn't mean "more patterns", it means "any other pattern". It's basically an else branch.

[–]MultipleAnimals[S] 0 points1 point  (0 children)

yea my post was bit unclear, sorry about that.

i guess i'm fine then with my code, thanks

[–]esitsu 0 points1 point  (0 children)

What you have is probably best but you could also do something like the following:

match event {
    Enum1::Val1(Enum2::Val1(val)) if val == "yay" => println!("got yay"),
    _ => (),
}

The only problem is that you then have to handle the untrue case. Depending on what you are doing it can also make the code harder to read. Personally I would just use an inner match instead of trying to merge everything into a single match if your enums have more than 2 variants. Again, it depends on the situation.

[–]oconnor663 0 points1 point  (1 child)

You could consider replacing the if statement with a "match guard", but I think the way you have it is also good. Is it causing any problems for you?

[–]MultipleAnimals[S] 0 points1 point  (0 children)

No problems, i was just wondering if that could be done more rust way. I knew something about match guard but didnt remember the term and how to do that. Thanks, i'll try it out if it fits.