The match expression in Rust compares a value against patterns to execute specific code blocks for each case.
The match expression compares a value against a series of patterns and executes the code block associated with the first pattern that matches.
fn main() {
let number = 13;
match number {
1 => println!("One"),
2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
_ => println!("Something else"),
}
}
The match expression is like a vending machine that checks what you put in and gives you a specific result based on that input. It ensures you handle every possible outcome, preventing your program from crashing if an unexpected value appears. You use it whenever you need to make decisions based on the specific shape or value of your data.