rust-unofficial / patterns

A catalogue of Rust design patterns, anti-patterns and idioms
https://rust-unofficial.github.io/patterns/
Mozilla Public License 2.0
8.11k stars 375 forks source link

Make "mem::replace to keep owned values in changed enums" example more general #38

Closed briansmith closed 7 years ago

briansmith commented 8 years ago

The example seems unrealistic because it uses if let instead of match. The example should use match since it is more general. Most of the cases where I would use this pattern would require the use of match.

cbreeden commented 8 years ago

Hmm, I tend to agree. The original PR used a match and then another PR simplified this particular example (and probably correctly so) to an if let. Perhaps it may be worthwhile to finding an example out in the wild unless someone else has another example in mind?

diwic commented 8 years ago

I use if let much more often than match.

nrc commented 8 years ago

I don't we can say match or if let is more general or more common, but as a matter of style I think that example should use match - where you have where there is both an if let and an else branch, it is nearly always better to use match.

briansmith commented 8 years ago

Maybe it's good to have two examples, one with if let and one with match. Then we can direct people to the specific example that most closely matches their code.