The if let
syntax lets you combine if
and let
into a less verbose way to handle values that match one pattern while ignoring the rest.
The syntax if let
takes a pattern and an expression separated by an equal sign. It works the same way as a match
, where the expression is given to the match and the pattern is its first arm.
You can think of if let
as syntax sugar for a match
that runs code when the value matches one pattern and then ignores all other values.
We can include an else
with an if let
. The block of code that goes with the else
is the same as the block of code that would go with the _
case in the match
expression that is equivalent to the if let
and else
.