Skip to content

Commit e093099

Browse files
authored
Merge pull request #1819 from stephenlb/patch-1
While-Let Unable to compile code example on page
2 parents 7c07def + efe239e commit e093099

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/flow_control/while_let.md

+18-16
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,26 @@ loop {
3131
Using `while let` makes this sequence much nicer:
3232

3333
```rust,editable
34-
// Make `optional` of type `Option<i32>`
35-
let mut optional = Some(0);
36-
37-
// This reads: "while `let` destructures `optional` into
38-
// `Some(i)`, evaluate the block (`{}`). Else `break`.
39-
while let Some(i) = optional {
40-
if i > 9 {
41-
println!("Greater than 9, quit!");
42-
optional = None;
43-
} else {
44-
println!("`i` is `{:?}`. Try again.", i);
45-
optional = Some(i + 1);
34+
fn main() {
35+
// Make `optional` of type `Option<i32>`
36+
let mut optional = Some(0);
37+
38+
// This reads: "while `let` destructures `optional` into
39+
// `Some(i)`, evaluate the block (`{}`). Else `break`.
40+
while let Some(i) = optional {
41+
if i > 9 {
42+
println!("Greater than 9, quit!");
43+
optional = None;
44+
} else {
45+
println!("`i` is `{:?}`. Try again.", i);
46+
optional = Some(i + 1);
47+
}
48+
// ^ Less rightward drift and doesn't require
49+
// explicitly handling the failing case.
4650
}
47-
// ^ Less rightward drift and doesn't require
48-
// explicitly handling the failing case.
51+
// ^ `if let` had additional optional `else`/`else if`
52+
// clauses. `while let` does not have these.
4953
}
50-
// ^ `if let` had additional optional `else`/`else if`
51-
// clauses. `while let` does not have these.
5254
```
5355

5456
### See also:

0 commit comments

Comments
 (0)