RRust By Example
beginnerFlow Control

If Let

Concise pattern matching for a single case.

If Let

Concise pattern matching for a single case.

Difficulty

Beginner

Code

rust
fn main() {
    let config_max: Option<u8> = Some(3);
    if let Some(max) = config_max {
        println!("max is {}", max);
    }

    let val: Result<i32, &str> = Ok(42);
    if let Ok(v) = val {
        println!("value: {}", v);
    }
}

Explanation

if let combines if and let for single-pattern matching.

Key Concepts

  • Read the code carefully and understand the data flow
  • Try modifying the example to see how it changes behavior
  • Run this code in the Rust Playground

Related Topics

Browse more examples in the flow-control category to build a complete understanding of this topic.

More Flow Control Examples