热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Rustiflet

iflet对于一些使用情况,匹配很尴尬。例如:Make`optional`oftype`Option`letoptionalSome(7);matchoptional{Some(i

if let

对于一些使用情况,匹配很尴尬。例如:

// Make `optional` of type `Option
`
let optiOnal= Some(7);
match optional {
Some(i) => {
println!("This is a really long string and `{:?}`", i);
// ^ Needed 2 indentations just so we could destructure
// `i` from the option.
},
_ => {},
// ^ Required because `match` is exhaustive. Doesn't it seem
// like wasted space?
};

if let 是清洁器用例,另外允许指定的各种失败选项:

fn main() {
// All have type `Option
`
let number = Some(7);
let letter: Option
= None; let emoticon: Option
= None; // The `if let` construct reads: "if `let` destructures `number` into // `Some(i)`, evaluate the block (`{}`). Else do nothing. if let Some(i) = number { println!("Matched {:?}!", i); } // If you need to specify a failure, use an else: if let Some(i) = letter { println!("Matched {:?}!", i); } else { // Destructure failed. Change the failure case. println!("Didn't match a number. Let's go with a letter!"); }; // Provide an altered failing condition. let i_like_letters = false; if let Some(i) = emoticon { println!("Matched {:?}!", i); // Destructure failed. Evaluated the condition to see if this branch // should be taken. } else if i_like_letters { println!("Didn't match a number. Let's go with a letter!"); // The condition evaluated false. This branch is the default. } else { println!("I don't like letters. Let's go with an emoticon :)!"); }; }

也可以参考:

enumOption, 和 RFC


    推荐阅读
    author-avatar
    可爱的你公馆_698
    这个家伙很懒,什么也没留下!
    PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有