gpt4 book ai didi

rust - 匹配语句是否按顺序执行并且它们可以重叠吗?

转载 作者:行者123 更新时间:2023-11-29 08:08:09 25 4
gpt4 key购买 nike

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

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

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

本质上,Rust 的 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 !"),
}

关于rust - 匹配语句是否按顺序执行并且它们可以重叠吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27078510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com