Pattern Matching
We have seen pattern matching in the previous example. It's a powerful feature in MoonBit that can be used in many places. It can help you test conditions conveniently and effectively, making your programs more precise and robust.
In this example, we give some basic use cases of pattern matching. Some other languages call it "destructuring" or "structured bindings", a way to extract values from a complex data structure.
"Destructuring" is just a subset of this feature. In MoonBit, almost every type you can construct can have a form to "destruct", which we call a pattern.
struct Point {
x : Int
y : Int
} derive(Show)
fn main {
let tuple = (1, false, 3.14)
let array = [1, 2, 3]
let record = { x: 5, y: 6 }
let (a, b, c) = tuple
println("a:\{a}, b:\{b}, c:\{c}")
let [d, e, f] = array
println("d:\{d}, e:\{e}, f:\{f}")
let { x, y } = record
println("x:\{x}, y:\{y}")
}