Add an `if` condition after a pattern in a `match` arm to execute code only when both the pattern matches and the condition is true.
Use a match guard by adding an if condition after a pattern in a match arm to filter matches based on a boolean expression.
match x {
Some(n) if n > 5 => println!("Greater than 5"),
Some(n) => println!("5 or less"),
None => println!("No value"),
}
The if n > 5 part is the match guard; the arm only executes if the pattern Some(n) matches AND the condition is true.
A match guard is like a bouncer at a club who checks your ID (the pattern) and then asks if you are on the guest list (the condition). It lets you run code only when a value matches a specific shape AND meets an extra requirement. This prevents you from writing messy nested if-statements inside your match arms.