XU XUv0.8.8
中文

Docs / Tutorial

08 · Enum and match

Tagged union. Construct with Color.Rgb(1, 2, 3). match must be exhaustive or have else. Matching an owning payload consumes the subject.

Source

@main

enum Color {
    Red
    Green
    Rgb(int, int, int)
}

fn show(c Color) {
    match c {
        Color.Red {
            println("red")
        }
        Color.Green {
            println("green")
        }
        Color.Rgb(r, g, b) {
            println(r)
            println(g)
            println(b)
        }
    }
}

fn main() int {
    show(Color.Red)
    show(Color.Green)
    show(Color.Rgb(1, 2, 3))
    << 0
}

Build (from the language repo root; swap the binary for your platform):

xu run examples/08_enum_match/main.xu
# or: xu build examples/08_enum_match/main.xu
# default artifact is ./main on mac/linux, ./main.exe on Windows

Run from the separate examples repository root; source files live under examples/.