Rust programming video tutorial (Advanced) – 024_ 1 syntax of all modes 1

Time:2021-9-19

Video address

Headline address:https://www.ixigua.com/i677586170644791348…
Station B address:https://www.bilibili.com/video/av81202308/

Source address

GitHub address:https://github.com/anonymousGiga/learn_rus…

Explanation content

1. Match literal value
example:

let x = 1;
match x {
    1 => println!("one"),
    2 => println!("two"),
    3 => println!("three"),
    _ => println!("anything"),
}

2. Match named variables
example:

fn main() {
    let x = Some(5);
    let y = 10;  // 1 place
    match x {
        Some(50) => println!("Got 50"),
        Some(y) => println! ("matched, y = {:?}", y), // y here is different from y at 1 above. Here is the introduced variable y covering y before
        _ => println!("Default case, x = {:?}", x),
    }
    println!("at the end: x = {:?}, y = {:?}", x, y);
}

3. Multiple modes
example:

let x = 1;
match x {
    1 | 2 => println!("one or two"),
    3 => println!("three"),
    _ => println!("anything"),
}

Note: | indicates or, i.e. matches 1 or 2

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Linghu rushed