Skip to content
Justice Tech School

if let and while let

if let matches one pattern without a full match. while let loops while a pattern keeps matching, such as popping a stack until empty.

Example
let maybe = Some(3);
if let Some(n) = maybe { println!("got {}", n); }
let mut stack = vec![1, 2, 3];
while let Some(top) = stack.pop() { print!("{} ", top); }
println!();

Result

got 3
3 2 1 

Exercise

Match one pattern.

if  Some(x) = opt {}

Quiz Test

What does while let do?

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