Rust programming video tutorial (Advanced) — 022 mode

Time:2021-7-18

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. Pattern is a special syntax in rust, which is used to match the structure of value.

2. The model consists of the following contents:
(1) Face value
(2) An array, enumeration, struct, or tuple that is deconstructed
(3) Variable
(4) Wildcard
(5) Place holder

3. In rust, you may use the location of the pattern
(1) Match branch
Matching method:

match VALUE {
    PATTERN => EXPRESSION,
    PATTERN => EXPRESSION,
    PATTERN => EXPRESSION,
}

Key point: all cases should be matched completely (that is, all cases must be exhausted), otherwise an error will be reported

example:

fn main() {
    let a = 1;
    match a {
        0 => println!("zero"),
        1 => println!("one"),
        _ =>  println!(" "Other", // if the sentence is commented out, an error will be reported
    };
}

(2) If let conditional expression
The if let expression is not checked by the compiler.
example:

fn main() {
    let color: Option<&str> = None;
    let is_ok = true;
    let age: Result<u8, _> = "33".parse();
    if let Some(c) = color {
        println!("color = {}", c);
    } else if is_ok {
        println!("is ok? {}", is_ok);
    } else if let Ok(a) = age {
        if a > 30 {
            println!("oh, mature man");
        } else {
            println!("oh, young man");
        }
    } else {
        println!("in else");
    }
}

(3)while let
The while loop is allowed as long as the pattern matches.
example:

fn main() {
    let mut stack = Vec::new(); 
    stack.push(1); 
    stack.push(2); 
    stack.push(3); 
    while let Some(top) = stack.pop() { 
        println!("{}", top); 
    }
}

In the above example, match some (value) all the time and loop as long as match.

(4) For loop
In the for loop, the pattern is the value that the for keyword follows directly, just as X in for X in y.
example:

fn main() {
    let v = vec!['a', 'b', 'c']; 
    for (index, value) in v.iter().enumerate() { 
        println!("{} is at index {}", value, index); 
    }
}

Note: the pattern here is (index, value). The enumerate method adapts an iterator to generate a value and its index in the iterator, which are in a tuple. The first enumerate call produces a tuple (0, ‘a’). When the value matches the pattern (index, value), the index will be 0 and the value will be “a”, and the first line of output will be printed.

(5) Let statement

let PATTERN = EXPRESSION;

example:

let (x, y, z) = (1,2,3);

Description: (1, 2, 3) matching pattern (x, y, z) will bind 1 to x, 2 to y and 3 to Z.
You can also use the_ Or

example:

let (x, .., y) = (1, 2, 3);
println!("x: {}, y: {}", x, y);

(6) Function parameters
The arguments to the function are also patterns.
example:

fn print_point(&(x, y): &(i32, i32)) { 
    println!("p: ({}, {})", x, y);
} 
fn main() { 
    let point = (3, 5); 
    print_point(&point); 
}

Note: the value (3,5) will match the pattern (x, y), so x gets the value 3, and Y gets the value 5.

4. Patterns do not work in the same way in every place where they are used; In some places, patterns must be irrefutable, meaning they must match any value provided. In other cases, it is refutable.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Linghu Yichong