If assign in Rust 1.55

Find this useful? Support us: Star on GitHub 6
Category: Conditional | Language: Rust 1.55

In Rust, if let syntax is used for conditional binding. It allows us to unpack a single item from a pattern, and if the pattern does not match, the code block will not execute.

Syntax for if let:

if let pattern = expression {
    // code to execute if pattern matches expression
}

Here is an example that demonstrates the use of if let:

fn main() {
    let x = Some(5);
    let y = None;
    
    // use if let to check if the value of x is Some
    if let Some(num) = x {
        println!("x: {}", num);
    }
    else {
        println!("x is None");
    }
    
    // use if let to check if the value of y is Some
    if let Some(num) = y {
        println!("y: {}", num);
    }
    else {
        println!("y is None");
    }
}

Output:

x: 5
y is None

In the above example, the first if let block executes because x contains a Some value. The second if let block does not execute because y is None.

if let can also be used to destructure tuples and enums. Here is an example:

enum Color {
    Red,
    Green,
    Blue,
}

fn main() {
let color = Color::Green;

// use if let to match against the Green variant
if let Color::Green = color {
println!("color is Green");
}
else {
println!("color is not Green");
}
}

Output:

color is Green

In the above example, if let is used to match against the Green variant of the Color enum. If the color variable contains the Green variant, the first code block will execute, otherwise the second code block will execute.