Guards and or-patterns
Separate top-level alternatives with |. Every alternative must bind the same names with the same types. Add if condition after a pattern to require a guard.
Guards run only after the pattern matches. They do not make a match exhaustive, so keep an unguarded case for every remaining value.
fn classify(value: int) -> string {
match value {
x if x < 0 => "negative",
0 | 1 | 2 => "small",
_ => "large",
}
}
fn main() {
println(classify(-1))
println(classify(2))
}
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: negative small
Ctrl/⌘+Enter runs the current lesson.