Using match with a compound type

A compound type is a type that contains many different types—a struct is possibly the simplest example here. The following also applies to enums and tuples.

We can match on a struct pattern the same way we can match on any other type of pattern, which is shown in the following example:

struct MyStruct 
{ 
    a: i32, 
    b: i32 
} 
 
fn derp(){ 
    let mystruct=MyStruct{a:1, b:2}; 
    match mystruct { 
        MyStruct{a, b} => println!("matched the structure"), 
        _ => println!("didn't match the structure") 
    } 
} 

As described in the matching section, we can ignore parameters in the struct pattern match using .., or just throw them away using _.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset