Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Latest commit

 

History

History
38 lines (26 loc) · 860 Bytes

File metadata and controls

38 lines (26 loc) · 860 Bytes

case

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

{% hint style="info" %} case expressions are compiled to if...else statements. {% endhint %}

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 */
}