What is the match expression in Rust?
Answer
The match expression is Rust's powerful pattern matching construct, similar to a switch statement but far more capable. It matches a value against a series of patterns and executes the corresponding arm. Match is exhaustive: the compiler forces you to handle every possible case — forgetting a variant is a compile error, not a runtime surprise. Patterns can match literal values, ranges (1..=5), enum variants with destructuring, tuples, structs, guards (if condition), and the catch-all _. Each arm can bind matched values to variables. For example, matching on Option<i32>: match value { Some(n) => println!("{}", n), None => println!("empty") }.
Previous
What are Rust enums and how do they differ from C enums?
Next
What is Option<T> in Rust and why does Rust have no null?