match keywordmatch evaluates code depending on a value of an enum.
fun say_hello(name: Option<String>) {
match name {
Some(n) => println("Hello " ^ n)
None => println("Hello there!")
}
}
say_hello(None) // Hello there!
say_hello(Some("Wilfred")) // Hello WilfredIf you want to ignore some cases, use _.
enum Flavor {
Chocolate,
Strawberry,
Vanilla,
}
fun describe_flavor(f: Flavor) {
match f {
Chocolate => println("Tasty!")
_ => {
println("Some other flavor.")
println("Could be strawberry or vanilla.")
}
}
}match also supports destructuring of tuples.