Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are match statements executed in order and can they overlap?

Tags:

rust

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?

like image 624
IdeaHat Avatar asked Nov 02 '25 15:11

IdeaHat


1 Answers

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 !"),
}
like image 160
Levans Avatar answered Nov 04 '25 14:11

Levans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!