Rust匹配析构元组

匹配块可在各种方式析构项目中。

元组

元组可以在匹配中被析构如下:

fn main() {
    let pair = (0, -2);
    // TODO ^ Try different values for `pair`

    println!("Tell me about {:?}", pair);
    // Match can be used to destructure a tuple
    match pair {
        // Destructure the second
        (0, y) => println!("First is `0` and `y` is `{:?}`", y),
        (x, 0) => println!("`x` is `{:?}` and last is `0`", x),
        _      => println!("It doesn't matter what they are"),
        // `_` means don't bind the value to a variable
    }
}

也可以看看:

元组

        原文作者:Rust教程
        原文地址: https://www.yiibai.com/rust/flow_control_match_destructuring_destructure_tuple.html
        本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
    点赞