匹配语句是按顺序执行的,它们是否可以重叠?

时间:2014-11-22 14:33:05

标签: rust

文档不清楚...... match语句中的情况是否保证按顺序执行?在无关心匹配的情况下,重叠匹配是否可以?

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

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

基本上,Rust的match可以用作一种奇怪的案例过滤器吗?

1 个答案:

答案 0 :(得分:2)

是的,匹配声明保证按顺序执行。这两个匹配是等价的:

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 !"),
}
相关问题