Expressions
expr ::= literal
| if_expr
| match_expr
Binary and unary operators
Conditions
block ::= "{" stmts "}"
condition ::= expr
| "let" pattern "=" expr
consequent ::= "if" condition block
alternative ::= "else" consequent ( alternative )?
| "else" block
if_expr ::= consequent ( alternative )?
If-let conditions
Sometimes it’s desirable to perform pattern matching on a value with a single pattern without needing the full power of the match
expression.
For these cases, the if-let expression can be useful.
Instead of a boolean condition, it uses the let
keyword and a single pattern:
let thing = Maybe::Just(123);
if let Maybe::Just(x) = thing {
-- use the `x` value
};
If-let conditions can be chained together although a match expression is likely to be more idiomatic and less verbose:
let thing = Maybe::Just(123);
if let Maybe::Just(42) = thing {
"the answer"
} else if let Maybe::Just(x) = thing {
"the value: " ++ ::Fmt::int(x)
} else {
"has no value"
};
-- vs --
match thing {
Maybe::Just(42) => "the answer",
Maybe::Just(x) => "the value: " ++ ::Fmt::int(x),
Maybe::None => "has no value",
};
Loops
Match expressions
match_arm_handler ::= expr
| block
match_arm ::= pattern "=>" match_arm_handler
match_expr ::= "match" "{" ( match_arm )+ "}"
See the Patterns chapter for information about how to construct patterns.