A case expression looks like this:

case (condition) {
match1 => value1
match2 => value2
match3 => value3
=> defaultValue

It returns the value of the first branch that matches the condition, or the default if none matched.

There are some rules that will be enforced by either the parser or the compiler:

  • the condition can be any type

  • the matches of branches must be the same type as the condition.

  • the values of the branches must be the same type

case expressions are compiled to if...else statements.

Matching an enum

Enums can be matched using a case expression to destructure it's variants values like so:

case (result) {
Result::Err error => /* do something with the error */
Result::Ok value => /* do something with the value */