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

Rust 匹配语句是否按顺序执行,它们是否可以重叠?,rust,Rust,文档对此不清楚,match语句中的案例是否保证按顺序执行?在不关心匹配的情况下,重叠匹配可以吗 let a: bool; let b: bool; let c: bool; let d: bool; match (a, b, c, d) { (true, _, _, _) => { /* ... */ } (_, true, _, _) => { /* ... */ } } 从本质上讲,Rust的match可以用作一种奇怪的案例过滤器吗?是的,match语句保证按

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

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

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

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

是的,match语句保证按顺序执行。这两个匹配是等效的:

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 !"),
}

有趣…如果你能提供自定义匹配器,这可能会产生一些非常非常奇怪的代码。