Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

If and Match

if

let y = if x == 3 "yes" else "no"

The if expression is a ternary operator. If the expression is true, it evaluates to the then case, otherwise it evaluates to the else case. Both cases must have the same type or it is a compiler error.

Not to be confused with the if statement.

match

Much like the match statement, a match expression evaluates to one of its arm expressions based on an equality test against an expression. If no expression matches, it matches a default case represented by _ if it exists. If _ is not present, the failed match causes a runtime exception.

action foo(x int) {
    let v = match x {
        3 => 1
        4 => 2
        _ => 0
    }
}

Each arm is a separate expression. Similarly to if expressions, each subordinate expression in a match must evaluate to the same type as the test expression or it is a compile error.

Not to be confused with the match statement.