【问题标题】:logical 'and' in matching branches匹配分支中的逻辑“和”
【发布时间】:2019-12-26 09:03:43
【问题描述】:

如何在match'手臂中表达逻辑'和'?

像下面这样:

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

【问题讨论】:

    标签: rust


    【解决方案1】:

    使用matchguard

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

    【讨论】:

    • String::from 是否比 .to_string().to_owned() 更好?是否可以在条件下使用s.trim() 的乘积作为返回值?如果Ok(s) if s.trim() != "" 手臂假定s.trim() 没有双重调用?
    • "是否可以使用条件中s.trim()的乘积作为返回值?"没有比赛后卫非常有限,为什么我从不使用它们
    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 2013-08-23
    相关资源
    最近更新 更多