Skip to content

Commit

Permalink
Merge pull request #1819 from stephenlb/patch-1
Browse files Browse the repository at this point in the history
While-Let Unable to compile code example on page
  • Loading branch information
marioidival authored Mar 8, 2024
2 parents 7c07def + efe239e commit e093099
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/flow_control/while_let.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,26 @@ loop {
Using `while let` makes this sequence much nicer:

```rust,editable
// Make `optional` of type `Option<i32>`
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<i32>`
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:
Expand Down

0 comments on commit e093099

Please sign in to comment.