Advanced match patterns and guards
In this section, we'll take a look at some of the advanced usage of match and let patterns. First, let's look at match.
Match guards
We can also use match guards on arms (if code > 400 || code <= 500
) to match on a subset of values. They start with an if
expression.
Advanced let destructure
We have the following complex data that we want to match against:
// complex_destructure.rs enum Foo { One, Two, Three } enum Bar(Foo); struct Dummy { inner: Bar } struct ComplexStruct { obj: Dummy } fn get_complex_struct() -> ComplexStruct { ComplexStruct { obj: Dummy { inner: Bar(Foo::Three) } } } fn main() { let a = get_complex_struct(); }