Rust- match匹配

The match expression in Rust is a powerful tool that allows you to handle multiple outcomes of a pattern match operation, very similar to a switch statement in other languages but way more powerful.

In its simplest form, it can be used to match values:

let value = 1;

match value {
    1 => println!("one"),
    2 => println!("two"),
    _ => println!("something else"),
}

In the above example, the variable value is compared with the patterns in each arm of the match expression. The _ pattern is a catch-all that matches anything, and it’s usually used as the last arm.

However, Rust’s match is capable of a lot more than just comparing values. It can destructure complex data types:

enum OptionalInt {
    Value(i32),
    Missing,
}

let x = OptionalInt::Value(5);

match x {
    OptionalInt::Value(i) if i > 5 => println!("Got an int bigger than five!"),
    OptionalInt::Value(..) => println!("Got an int!"),
    OptionalInt::Missing => println!("No such int!"),
}

In this example, the match expression is used to handle the various possible states of an OptionalInt enum.

Rust also supports pattern guards, allowing for more complex logic:

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

match pair {
    (x, y) if x == y => println!("These are twins"),
    (x, y) if x + y == 0 => println!("These numbers cancel each other out"),
    (x, _) if x % 2 == 1 => println!("The first one is odd"),
    _ => println!("No correlation..."),
}

In this example, match evaluates pairs of numbers and uses pattern guards to add conditions to the patterns.

In summary, match is a versatile tool in Rust that can be used to write clear and concise code. It enforces exhaustive checking, ensuring that all possibilities are handled.

A comprehensive case is as follows:

fn main() {
    /*
       解构 &、ref、ref mut
       解引用 *
    */

    let num = &100;

    match num {
        &val => println!("&val 是 {:?}", val), // &val 是 100
    }

    match *num {
        val => println!("val 是 {:?}", val), // val 是 100
    }

    // ref 改变了赋值的行为,可以对具体值创建引用
    let ref num3 = 66;

    // 定义2个非引用变量,通过ref和ref mut仍然可以得到其引用。
    let num4 = 5;
    let mut mut_num4 = 7;

    match num4 {
        ref r => println!("num4 是 {:?}", r), // num4 是 5
    }

    match mut_num4 {
        ref mut m => {
            *m += 10;
            println!("mut_num4 是 {:?}", m); // mut_num4 是 17
        }
    }

    let s = Study {
        name: String::from("Rust"),
        target: String::from("熟练书写Rust程序"),
        spend: 36,
    };

    let Study {
        name: name,
        target: target,
        spend: spend,
    } = s;

    println!("name = {}, target = {}, spend = {}", name, target, spend);
    // name = Rust, target = 熟练书写Rust程序, spend = 36

    let s2 = Study {
        name: String::from("Rust"),
        target: String::from("熟练书写Rust程序"),
        spend: 36,
    };

    let Study { name, .. } = s2;
    println!("name = {}", name); // name = Rust
}

struct Study {
    name: String,
    target: String,
    spend: u32,
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青衫客36

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值