Skip to main content

控制流

if


let num = 1;

if num < 5 {

}

// 如果代码有多个 else if 请使用 match 重构

// 由于 if 是个表达式 所以可以放在等号的右边

let condition = true;

let number = if condition { 5 } else { 6 };
区别
  • JS 的条件需要加小括号
  • JS 的条件可以不是 boolean 类型,因为有隐式类型转换。 Rust 必须是 bool 类型
  • Rustif else 表达式返回结果必须是同一类型

match

  • 允许一个值与一系列模式进行匹配,并执行匹配的模式对应的代码。
  • 模式可以是字面值、变量名、通配符...
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn main() {}

fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 15,
}
}

绑定值的模式

  • 匹配的分支可以绑定到被匹配对象的部分值
    • 因为,可以从 enum 变体中提取值。
#[derive(Debug)]
enum UsState {
Alaska,
Alabama,
}

enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}

fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => {
println!("Penny");
1
}
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("{:#?}", state);
25
}
}
}

fn main() {
let q = Coin::Quarter(UsState::Alaska);

println!("{}", value_in_cents(q));
}

变体模式

fn main() {
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);

println!("six {:?}", six);
println!("none {:?}", none);
}

fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
tip

match 匹配必须穷举所有的可能

举例
fn main() {
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);

println!("six {:?}", six);
println!("none {:?}", none);
}

fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
// None => None, missing match arm: `None` not covered
Some(i) => Some(i + 1),
}
}
_ 通配符
// 替代其余没列出的值
fn main() {
let v = 21u8; // u8的值有256种 0 - 255

match v {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (),
}
}

枚举与模式匹配 if let

  • 处理只关心一种匹配而忽略其他匹配的情况
fn main() {
let v = Some(21u8); // u8的值有256种 0 - 255

match v {
Some(3) => println!("three"),
_ => println!("others"),
}

// 等同于上面的代码 但是更简洁
if let Some(3) = v {
println!("three");
} else {
println!("others");
}
}
  • 更少的代码,更少的缩进,更少的模板代码。
  • 放弃了穷举的可能。
  • 可以把 if let 看作是 match 的语法糖。
  • 搭配 else