If-let and Match
https://doc.rust-lang.org/reference/expressions/if-expr.html
Patterns can be used as conditions in if, we call these if-lets:
#![allow(unused)] fn main() { if let Point { 3, .. } = my_point { // do something } else { // do something esle } }
The Rust equivalent of a switch is called match. Unlike constants,
match actually takes patterns for its arms
#![allow(unused)] fn main() { fn point_function(p: Point) { match n { Number { 0.0, _ } => println!("point is on the y axis"), Number { _, 0.0 } => println!("point is on the x axis"), Number { _, _ } => println("point is not on any axis"); } } }
A match has to be exhaustive: at least one arm needs to match for every value,
otherwise, you'd get an error. If there are cases you want to ignore you can add
an explicit match arm doing nothing and matching everything else, such as _ => {}.
Many things in Rust are patterns, including identifiers, literals and so on.