Remaining flow control in RBE
This commit is contained in:
parent
dacbddca6e
commit
b94fb3b31e
16
rustbyexample/flow_control.d/let_else.rs
Normal file
16
rustbyexample/flow_control.d/let_else.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
fn get_count_item(s: &str) -> (u64, &str) {
|
||||||
|
let mut it = s.split(' ');
|
||||||
|
let (Some(count_str), Some(item)) = (it.next(), it.next()) else {
|
||||||
|
panic!("Can't segment count item pair: '{s}'");
|
||||||
|
};
|
||||||
|
let Ok(count) = u64::from_str(count_str) else {
|
||||||
|
panic!("Can't parse integer: '{count_str}'");
|
||||||
|
};
|
||||||
|
(count, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert_eq!(get_count_item("3 chairs"), (3, "chairs"));
|
||||||
|
}
|
13
rustbyexample/flow_control.d/while_let.rs
Normal file
13
rustbyexample/flow_control.d/while_let.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
fn main() {
|
||||||
|
let mut optional = Some(0);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user