diff --git a/src/flow_control/while_let.md b/src/flow_control/while_let.md index 745b7ae75f..f6f8eff2e0 100644 --- a/src/flow_control/while_let.md +++ b/src/flow_control/while_let.md @@ -31,24 +31,26 @@ loop { Using `while let` makes this sequence much nicer: ```rust,editable -// Make `optional` of type `Option` -let mut optional = Some(0); - -// This reads: "while `let` destructures `optional` into -// `Some(i)`, evaluate the block (`{}`). Else `break`. -while let Some(i) = optional { - if i > 9 { - println!("Greater than 9, quit!"); - optional = None; - } else { - println!("`i` is `{:?}`. Try again.", i); - optional = Some(i + 1); +fn main() { + // Make `optional` of type `Option` + let mut optional = Some(0); + + // This reads: "while `let` destructures `optional` into + // `Some(i)`, evaluate the block (`{}`). Else `break`. + while let Some(i) = optional { + if i > 9 { + println!("Greater than 9, quit!"); + optional = None; + } else { + println!("`i` is `{:?}`. Try again.", i); + optional = Some(i + 1); + } + // ^ Less rightward drift and doesn't require + // explicitly handling the failing case. } - // ^ Less rightward drift and doesn't require - // explicitly handling the failing case. + // ^ `if let` had additional optional `else`/`else if` + // clauses. `while let` does not have these. } -// ^ `if let` had additional optional `else`/`else if` -// clauses. `while let` does not have these. ``` ### See also: