Destrutturazione di Enum
Destructuring is a way of extracting data from a data structure by writing a pattern that is matched up to the data structure, binding variables to subcomponents of the data structure.
You can destructure tuples and arrays by matching on their elements:
Tuple
fn main() { describe_point((1, 0)); } fn describe_point(point: (i32, i32)) { match point { (0, _) => println!("on Y axis"), (_, 0) => println!("on X axis"), (x, _) if x < 0 => println!("left of Y axis"), (_, y) if y < 0 => println!("below X axis"), _ => println!("first quadrant"), } }
Array
#[rustfmt::skip] fn main() { let triple = [0, -2, 3]; println!("Tell me about {triple:?}"); match triple { [0, y, z] => println!("First is 0, y = {y}, and z = {z}"), [1, ..] => println!("First is 1 and the rest were ignored"), _ => println!("All elements were ignored"), } }
This slide should take about 5 minutes.
- Create a new array pattern using
_
to represent an element. - Aggiungi più valori all’array.
- Point out that how
..
will expand to account for different number of elements. - Show matching against the tail with patterns
[.., b]
and[a@..,b]