@@ -31,26 +31,24 @@ loop {
31
31
Using ` while let ` makes this sequence much nicer:
32
32
33
33
``` rust,editable
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.
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);
50
46
}
51
- // ^ `if let` had additional optional `else`/`else if`
52
- // clauses. `while let` does not have these .
47
+ // ^ Less rightward drift and doesn't require
48
+ // explicitly handling the failing case .
53
49
}
50
+ // ^ `if let` had additional optional `else`/`else if`
51
+ // clauses. `while let` does not have these.
54
52
```
55
53
56
54
### See also:
0 commit comments