Enums and Pattern Matching

Enums let you define a type by enumerating its possible variants. They are a core tool for modeling state and making illegal states unrepresentable. Oxide uses match with -> arms and _ as the wildcard.

What You'll Learn

  • How to define enums and attach data to variants
  • How to use match to branch on variants
  • How to write concise control flow with if let

A Quick Example

public enum Message {
    Quit,
    Move { x: Int, y: Int },
    Write(String),
}

fn describe(msg: Message): String {
    match msg {
        Message.Quit -> "Quit message",
        Message.Move { x, y } -> "Move to \(x), \(y)",
        Message.Write(text) -> "Text: \(text)",
        _ -> "Unknown message",
    }
}

In the following sections, you'll see how enums and pattern matching work together to express rich, safe control flow.