if let
fn main() {
// 全部都是 `Option<i32>` 类型
let number = Some(7);
let letter: Option<i32> = None;
let emoticon: Option<i32> = None;
// `if let` 结构解读:若 `let` 将 `number` 解构成 `Some(i)`,则执行
// 语句块(`{}`)
if let Some(i) = number {
// 如果要指明失败情形,就使用 else:
if let Some(i) = letter {
println!("Matched {:?}!", i);
} else {
// 解构失败。换到失败情形(Change to the failure case)。
println!("Didn't match a number. Let's go with a letter!");
};
let i_like_letters = false;
if let Some(i) = emoticon {
println!("Matched {:?}!", i);
// 解构失败。执行 `else if` 条件来判断轮到的失败分支是否需要执行
} else if i_like_letters {
println!("Didn't match a number. Let's go with a letter!");
} else {
// 条件执行错误。这是默认的分支:
println!("I don't like letters. Let's go with an emoticon :)!");
}