@@ -31,24 +31,26 @@ loop {
31
31
Using ` while let ` makes this sequence much nicer:
32
32
33
33
``` 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.
46
50
}
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 .
49
53
}
50
- // ^ `if let` had additional optional `else`/`else if`
51
- // clauses. `while let` does not have these.
52
54
```
53
55
54
56
### See also:
0 commit comments