Trust learning — using if let to realize simple control flow

Time:2021-3-9

If let syntax allows you to combine if and let in a less lengthy way to handle values that match one pattern, ignoring the rest. For example, the program matches the option < U8 > value, but only wants to execute the code when the value is 3:

fn main() {
    let some_u8_value = Some(0u8);
    match some_u8_value {
        Some(3) => println!("three"),
        _  =>(println,
    }
}

We want to do something with the match of some (3), but nothing with other values of some < U8 > or none. To satisfy the match expression, we only need to deal with one variant and add it_ =>(), but a lot of template code needs to be added.
Instead, we can use if let to write this code in a shorter way and achieve a completely consistent matching effect:

fn main() {
    let some_u8_value = Some(8u8);
    if let Some(3) = some_u8_value {
        println!("three");
    }

    if let Some(8) = some_u8_value {
        Println;
    }
}

D:\learn\cargo_learn>cargo run
   Compiling cargo_learn v0.1.0 (D:\learn\cargo_learn)
    Finished dev [unoptimized + debuginfo] target(s) in 0.27s
     Running `target\debug\cargo_learn.exe`
It's a match

Correspondingly, if we want to get an exact result, for example, if we don’t match the one we need, we want to return a result, then we can use theelse:

#[derive(Debug)]
enum UsState {
    Alabama,
    Alaska,
}

enum Coin {
    Penny,
    Quarter(UsState),
}

fn main() {
    let coin = Coin::Penny;
    let mut count = 0;
    if let Coin::Quarter(state) = coin {
        println!("State quarter from {:?}!", state);
    } else {
        count += 1;
        Println! "Count will automatically + 1 if no result is matched!"!!! ")
    }
}


D:\learn\cargo_learn>cargo run
   Compiling cargo_learn v0.1.0 (D:\learn\cargo_learn)
warning: variable `count` is assigned to, but never used
  --> src\main.rs:14:9
   |
14 |     let mut count = 0;
   |         ^^^^^^^^^
   |
   = note: `#[warn(unused_variables)]` on by default
   = note: consider using `_count` instead

warning: value assigned to `count` is never read
  --> src\main.rs:18:9
   |
18 |         count += 1;
   |         ^^^^^
   |
   = note: `#[warn(unused_assignments)]` on by default
   = help: maybe it is overwritten before being read?

warning: variant is never constructed: `Alabama`
 --> src\main.rs:3:5
  |
3 |     Alabama,
  |     ^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: variant is never constructed: `Alaska`
 --> src\main.rs:4:5
  |
4 |     Alaska,
  |     ^^^^^^

warning: variant is never constructed: `Quarter`
 --> src\main.rs:9:5
  |
9 |     Quarter(UsState),
  |     ^^^^^^^^^^^^^^^^

warning: 5 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.51s
     Running `target\debug\cargo_learn.exe`
If no result is matched, count will automatically + 1!!!

Recommended Today

Rust and python: why rust can replace Python

In this guide, we compare the rust and python programming languages. We will discuss the applicable use cases in each case, review the advantages and disadvantages of using rust and python, and explain why rust might replace python. I will introduce the following: What is rust? What is Python? When to use rust When to […]