Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get argument of a variant?

Having these enums

pub enum Symbol {
    X,
    O,
}

pub enum CellContent {
    Move(Symbol),
    Empty,
}

and

let cell_content = CellContent::Move(Symbol::X);

how can I get the Symbol ? Of course if it’s of variant Move(Symbol)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This doesn’t work

        if cell_0_0 == Move(a_symbol) {  
            return Some(a_symbol);
        }

I cannot also do the following because I must do nothing (code must continue evaluation); and the following doesn’t even compile at all !

        match cell_0_0 {
            Move(symbol) => return symbol;
            _ => // do nothing;
        }

        // code must go on to check further conditions

| I still have some problem with basic syntax of rust, so I’m experiencing making some basic programs

So question is

How to

  • check if my variable is a variant of Move(Symbol)
  • if yes return (a copy of) symbol
  • else do nothing, so code can go on and do more checks
    ?

Edit 1: Full (not working) code

pub fn some_one_win(&self) -> Option<Symbol> {
    let cell_0_0: CellContent = self.table[0][0];
    let cell_0_1: CellContent = self.table[0][1];
    let cell_0_2: CellContent = self.table[0][2];
    if cell_0_0 == cell_0_1 && cell_0_0 == cell_0_2 {
        match cell_0_0 {
            Move(symbol) => return symbol;
            _ => // how to 'do nothing' here ?;
        }
    }
    let cell_1_0: CellContent = self.table[1][0];
    let cell_1_1: CellContent = self.table[1][1];
    let cell_1_2: CellContent = self.table[1][2];
    if cell_1_0 == cell_1_1 && cell_1_0 == cell_1_2 {
        match cell_1_0 {
            Move(symbol) => return symbol;
            _ => // how to 'do nothing' here ?;
        }
    }
    ... and so on ..
}

>Solution :

I cannot also do the following because I must do nothing (code must continue evaluation); and the following doesn’t even compile at all !

match cell_0_0 {
    Move(symbol) => return symbol;
    _ => // do nothing;
}

You can do that, if you get the syntax right:

match cell_0_0 {
    CellContent::Move(symbol) => {
        return Some(symbol);
    }
    _ => {}  // do nothing
}

But as PitaJ mentioned, when there’s only one pattern plus _, the if let construct is usually a cleaner alternative:

if let CellContent::Move(symbol) = cell_0_0 {
    return Some(symbol);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading