Compare commits
2 Commits
6e50ec65f8
...
b94fb3b31e
Author | SHA1 | Date | |
---|---|---|---|
b94fb3b31e | |||
dacbddca6e |
25
rustbyexample/flow_control.d/if_let.rs
Normal file
25
rustbyexample/flow_control.d/if_let.rs
Normal file
@ -0,0 +1,25 @@
|
||||
fn main() {
|
||||
let number = Some(7);
|
||||
let letter: Option<i32> = None;
|
||||
let emoticon: Option<i32> = None;
|
||||
|
||||
if let Some(i) = number {
|
||||
println!("Matched {:?}!", i);
|
||||
}
|
||||
|
||||
if let Some(i) = letter {
|
||||
println!("Matched {:?}!", i);
|
||||
} else {
|
||||
println!("Didn't match a number. Let's go with a letter!");
|
||||
}
|
||||
|
||||
let i_like_letters = false;
|
||||
|
||||
if let Some(i) = emoticon {
|
||||
println!("Matched {:?}!", i);
|
||||
} else if i_like_letters {
|
||||
println!("Didn't match a number. Let's go with a letter!");
|
||||
} else {
|
||||
println!("I don't like letters. Let's go with an emoticon :)!");
|
||||
}
|
||||
}
|
27
rustbyexample/flow_control.d/if_let2.rs
Normal file
27
rustbyexample/flow_control.d/if_let2.rs
Normal file
@ -0,0 +1,27 @@
|
||||
enum Foo {
|
||||
Bar,
|
||||
Baz,
|
||||
Qux(u32),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = Foo::Bar;
|
||||
let b = Foo::Baz;
|
||||
let c = Foo::Qux(100);
|
||||
|
||||
if let Foo::Bar = a {
|
||||
println!("a is a foobar");
|
||||
}
|
||||
|
||||
if let Foo::Bar = b {
|
||||
println!("b is foobar");
|
||||
}
|
||||
|
||||
if let Foo::Qux(value) = c {
|
||||
println!("c is {}", value);
|
||||
}
|
||||
|
||||
if let Foo::Qux(value @ 100) = c {
|
||||
println!("c is one hundred (value: {:?})", value);
|
||||
}
|
||||
}
|
11
rustbyexample/flow_control.d/if_let3.rs
Normal file
11
rustbyexample/flow_control.d/if_let3.rs
Normal file
@ -0,0 +1,11 @@
|
||||
enum Foo {
|
||||
Bar,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = Foo::Bar;
|
||||
|
||||
if let Foo::Bar = a {
|
||||
println!("a is foobar");
|
||||
}
|
||||
}
|
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