Match Statements
Match statements provide powerful pattern matching capabilities for control flow.
Basic Match
Section titled “Basic Match”import "std/io";
let status := 200;
match status { 200 => io::Println("OK"), 404 => io::Println("Not Found"), 500 => io::Println("Server Error"), _ => io::Println("Unknown Status"),}Pattern Types
Section titled “Pattern Types”Match supports three types of patterns:
Value Patterns
Section titled “Value Patterns”Match against specific values:
import "std/io";
let code := 42;
match code { 0 => io::Println("Zero"), 42 => io::Println("The Answer"), 100 => io::Println("Century"), _ => io::Println("Other"),}Type Patterns with is
Section titled “Type Patterns with is”Match against types using the is operator:
import "std/io";
let value: any = 42;
match value { is i32 => io::Println("Integer"), is str => io::Println("String"), is bool => io::Println("Boolean"), _ => io::Println("Unknown type"),}Range Patterns with in
Section titled “Range Patterns with in”Match against ranges using the in operator:
import "std/io";
let score := 85;
match score { in 90..=100 => io::Println("A"), in 80..=89 => io::Println("B"), in 70..=79 => io::Println("C"), in 0..=69 => io::Println("F"), _ => io::Println("Invalid score"),}Both .. (exclusive) and ..= (inclusive) range operators are supported.
Pattern Matching with Enums
Section titled “Pattern Matching with Enums”import "std/io";
type Status enum { Pending, Active, Done,};
let status := Status::Active;
match status { Status::Pending => io::Println("Waiting"), Status::Active => io::Println("In Progress"), Status::Done => io::Println("Complete"),}Important Notes
Section titled “Important Notes”- Match is a statement, not an expression - it cannot be used as a value
- All cases must be handled either explicitly or with a wildcard
_pattern - The
isandinoperators are only available in match statements, not in if-else conditions - For simple value comparisons in if-else, use regular equality operators