lundi 8 mars 2021

rust: match tuple value inside option

I want to match a vector of tuples and it's content on a single check. If the value inside the tuple is equal to some_value (a usize) then I do something, for every other case I do something else.

Cuurently I handle it like this with basic logic:

if myvector.is_empty() {
  // do action1
} else if myvector.last().unwrap().0 == some_value {
  // do action 2
} else {
  // do action1
}

This does what I want but I feel there's a more "rusty way to do it. I've been trying with match as such:

match myvector.last() {
  Some(t) => 
    match t.0 == some_value {
      true => 
        // do action1,
      false => 
        // do action2,
    },
  None => 
    // do action1,
}

This also works. But I'm trying to figure out a better syntax to cover a single case once only (aka action1).

Aucun commentaire:

Enregistrer un commentaire