gpt4 book ai didi

rust - 匹配分支中的逻辑 'and'

转载 作者:行者123 更新时间:2023-12-03 11:35:59 26 4
gpt4 key购买 nike

如何在match的 ARM 中表达逻辑“和”?

如下所示:

fn main() {
let expr: Result<_, ()> = Ok(String::from("hello"));
let _res = match expr {
Ok(s) && s.trim() != "" => s,
_ => String::from("None"),
};
}

最佳答案

使用match guard:

fn main() {
let expr: Result<_, ()> = Ok(String::from("hello"));
let _res = match expr {
Ok(s) if s.trim() != "" => s,
_ => String::from("None"),
};
}

关于rust - 匹配分支中的逻辑 'and',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59486107/

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