mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-15 10:02:42 +00:00
29 lines
744 B
Rust
29 lines
744 B
Rust
// ./src/flow_control/while_let.md
|
|
|
|
|
|
fn part0() {
|
|
// 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.
|
|
}
|
|
// ^ `if let` had additional optional `else`/`else if`
|
|
// clauses. `while let` does not have these.
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
}
|
|
|