IdeaHat
IdeaHat

Reputation: 7881

Are match statements executed in order and can they overlap?

The documentation isn't clear on this...are the cases in a match statement guaranteed to be executed in order? In the case of don't-care matches, is it OK to have overlapping matches?

let a: bool;
let b: bool;
let c: bool;
let d: bool;

match (a, b, c, d) {
    (true, _, _, _) => { /* ... */ }
    (_, true, _, _) => { /* ... */ }
}

Essentially, can Rust's match be used as a weird sort of case filter?

Upvotes: 4

Views: 2244

Answers (1)

Levans
Levans

Reputation: 15002

Yes the match statements are guaranteed to be executed in order. These two matches are equivalent:

match (a, b) {
    (true, _) => println!("first is true !"),
    (_, true) => println!("second is true !"),
    (_, _) => println!("none is true !"),
}

match (a, b) {
    (true, _) => println!("first is true !"),
    (false, true) => println!("second is true !"),
    (false, false) => println!("none is true !"),
}

Upvotes: 3

Related Questions