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,
}

你可能感兴趣的:(Rust,rust)