Pattern matching
Values and ranges
switch on numbers, text, characters and bool: literals, several values in one case, ranges, and binding the value.
The simplest patterns are literal values and ranges.
fn classify(n: int) -> string {
switch n {
case 0: "zero"
case 1, 2, 3: "small"
case 4..10: "medium"
case big if big > 1000: "huge"
case ..0: "negative"
default: "large"
}
}
fn main() {
for n of [0, 2, 5, 5000, -3, 50] { print(classify(n)) }
}zero
small
medium
huge
negative
largeForms
| Pattern | Matches |
|---|---|
case 0 | That value. |
case 1, 2, 3 | Any of several values. |
case 4..10 | A range, from 4 up to but not including 10. |
case ..0 | Everything below 0. |
case name | Anything, binding it to name. |
case name if condition | Anything for which the condition holds; see Guards. |
Ranges follow the same rules as in for.
Text, characters and bool
fn main() {
const s = "hi"
print(switch s { case "hi", "hello": 1 default: 0 })
const c = 'x'
switch c {
case 'a'..'m': print("first half")
case 'n'..'z': print("second half")
default: print("other")
}
switch true { case true: print("t") case false: print("f") }
}1
second half
tA bool has two values, so covering both needs no default.
Integers need a default
An integer has too many values to list, so a switch over one must end with default (or a binding case), otherwise it is an error:
fn main() {
const x = 5
switch x {
case 1..3: print("a")
}
}main:3:12: error: this `switch` does not handle `_`; add a case for it or a `default`