January 12, 2021 — rust, language design
One of my favourite features of Rust is that it has excellent support for pattern matching. This is usually done through match expressions, but there is also a shorter syntax that can be used if you only need a single branch, which is called if let.
This shortcut syntax, useful as it is, can be quite confusing to new Rust developers. I commonly see people asking things like:
I had similar questions when I started learning Rust, and to answer them, we need to take a step back and look at the plain old let syntax. It's relevant, I promise!
What might not be apparent at first glance is that the left side of a let isn't just a name - it's a pattern, the same as what you'd use in the arm of a match expression!