0PricingLogin
Learn Rust Coding · Lesson

if let and while let

Concise matching.

Concise Matching

Sometimes you care about just one pattern. if let and while let give you concise matching without a full match.

The if let Form

if let runs a block only when a value matches a pattern, binding any captured data.

fn main() {
    let maybe = Some(10);
    if let Some(n) = maybe {
        println!("value is {n}");
    }
}

All lessons in this course

  1. match Deep Dive
  2. if let and while let
  3. Binding with @
  4. Destructuring
← Back to Learn Rust Coding