Binding Patterns
Binding patterns not only match values but also bind the matched value to a variable, allowing you to use that value in the branch. The syntax is case variable_name =>, where the variable name can be any identifier you want.
Binding patterns are typically used to match and handle remaining cases. Unlike the wildcard _, binding patterns let you access the matched value. case other => will match any value and bind it to the other variable.
Note
Binding patterns cannot be used with
|, because each pattern needs to independently bind variables and cannot share them.Try changing code on line 2 to different values and observe how the binding pattern captures and uses these values.