Ignoring a parameter with match

It is entirely possible to ignore a parameter within a match construct. Take the following struct:

struct Test  
{ 
    answer: i32, 
    real_answer: i32, 
    score: i32,
} 

We can use this struct within a match construct as we can any other type. However, we want to ignore anything after real_answer. To do this, we will use the .. operator. Our match will look like this:

fn match_test(t: Test) 
{ 
    match t 
    { 
        Test {answer: Question::MyAnswer, real_answer: 
Question::RealAnswer, ..} => {...} } }

We can also use _ as a parameter (we expect a value, but we don't care what it is):

fn match_test(t:Test) 
{ 
    match t 
    { 
        Test {answer: Question::MyAmswer, real_answer: 
Question::RealAnswer, score:_} => {...} } }

You can appreciate that the match construct is powerful, but let's see it in action with patterns.

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

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