Tsallaka zuwa abun ciki
Justice Tech School

if let and while let

if let matches one pattern in a short form. while let loops while a pattern matches.

Example
fn main() {
    let o = Some(5);
    if let Some(x) = o { println!("got {}", x); }
    let mut st = vec![1, 2, 3];
    while let Some(t) = st.pop() { print!("{} ", t); }
    println!();
}

Result

got 5
3 2 1 

Exercise

Short match.

if  Some(x) = o {}

Quiz Test

What does if let do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.