Enum+match: Get compile warning when i expect an error

To elaborate even more, note that in Rust, let bindings allow a pattern on the left-hand side, not just a simple identifier, as long as that pattern is irrefutable, that is, can match any value the right-hand side may have. With that in mind, you can see that the most basic form of let that we're all familiar with, let x = y; is simply what you get when you put a wildcard pattern on the left-hand side! I find this quite elegant.

So what happens if you do this:

enum X { A, B }
use X::A;
let A = B;

I actually had to check to make sure, but it is indeed consistent with match: the compiler refuses to compile that because A is a refutable pattern referring to the enum variant A!

3 Likes