文档不清楚...... match
语句中的情况是否保证按顺序执行?在无关心匹配的情况下,重叠匹配是否可以?
let a: bool;
let b: bool;
let c: bool;
let d: bool;
match (a, b, c, d) {
(true, _, _, _) => { /* ... */ }
(_, true, _, _) => { /* ... */ }
}
基本上,Rust的match
可以用作一种奇怪的案例过滤器吗?
答案 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 !"),
}