quinnwencn / blog

Apache License 2.0
0 stars 0 forks source link

[RustLearning] match的使用 #38

Open quinnwencn opened 4 weeks ago

quinnwencn commented 4 weeks ago

在Rust中,match是一個非常便利的語法,使用得當可以大大減少代碼複雜度,也方便閱讀。match的使用主要以以下幾個場景爲主:

  1. 匹配枚舉值
    
    #[derive(Debug)] // so we can inspect the state in a minute
    enum UsState {
    Alabama,
    Alaska,
    // --snip--
    }

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

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

2. 匹配Option<T>
```Rust
    fn plus_one(x: Option<i32>) -> Option<i32> {
        match x {
            None => None,
            Some(i) => Some(i + 1),
        }
    }

    let five = Some(5);
    let six = plus_one(five);
    let none = plus_one(None);
  1. 匹配常量

    let dice_roll = 9;
    match dice_roll {
        3 => add_fancy_hat(),
        7 => remove_fancy_hat(),
        other => move_player(other),
    }
    
    fn add_fancy_hat() {}
    fn remove_fancy_hat() {}
    fn move_player(num_spaces: u8) {}

    使用match有一個限製:必須覆蓋所有分支!上述例子中無論是枚舉、Option還是常量,都覆蓋了所有分支,對於其他分支,通常是使用下劃線_標識,表示其餘所有情況。在匹配變量時,有一種用法,結合if語句使用:

    fn print_according_to_len(n: u16) -> String {
    match n {
        t if t % 3 == 0 ==> format!("{} can be divided by 3", t),
        t if t % 5 == 0 ==> format!("{} can be divided by 5", t),
        _ => format!("{} can''t  be divided by either 3 or 5", t),
    }
    }
quinnwencn commented 3 weeks ago

在match匹配时,如果有多种匹配情况执行同一个操作,这个或采用的是|,而不是||,这个和常见的C++的或不太一样:Matching Brackets