Rust programming video tutorial (Advanced) — 022 mode


Video address

Headline address:…
Station B address:

Source address

GitHub address:…

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 {

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


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.

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.

fn main() {
    let mut stack = Vec::new(); 
    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.

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 (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


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

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

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

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