while let
Statements
Like with if let
, there is a
while let
variant which repeatedly tests a value against a pattern:
Here
String::pop
returns Some(c)
until the string is empty, after which it will return None
.
The while let
lets us keep iterating through all items.
Speaker Notes
- Point out that the
while let
loop will keep going as long as the value matches the pattern. - You could rewrite the
while let
loop as an infinite loop with an if statement that breaks when there is no value to unwrap forname.pop()
. Thewhile let
provides syntactic sugar for the above scenario. - This form cannot be used as an expression, because it may have no value if the condition is false.