October 2018
Beginner
180 pages
4h 48m
English
We can use pattern matching to pull a value out of a complex data structure. As long as the pattern matches the data value, it doesn't matter how complex the pattern and value are. If we want to match a tuple of tuples and extract one particular value from one of the inner tuples, we can do it like this:
let (_, (_, x, _, _), _) = ((5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15));println!("x is {}", x);
This pattern matches any tuple of three elements, where the second element is a nested tuple of four elements, and stores the second element of that nested tuple in the x variable, then prints out the value of x.
We could have been even more specific, and replaced some of those _ with more detailed sub-patterns to match. That would ...
Read now
Unlock full access