And Godbolt tells me that this pattern compiles down to a single branch, as I'd expect. The part I don't understand is how the compiler knows that. Looking into the source of then and unwrap_or tells me that this will inline into something like,
But at this point I lose the compiler's logic and can't see what rule or transform it uses to realize the result of the match is logically equivalent to the condition and only needs to be checked once. Am I missing something or looking at this at too high a level?
I wonder if there's a MIR optimization that handles this or if it would make sense to write one. Even the simplified version (match on an option you just produced from a match) seems pretty common (think for example of for loops, they match on an option that the Iterator::next just produced, usually by checking some other condition)