mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-15 01:52:42 +00:00
45 lines
1 KiB
Rust
45 lines
1 KiB
Rust
// ./src/error/option_unwrap.md
|
|
|
|
|
|
// The adult has seen it all, and can handle any drink well.
|
|
// All drinks are handled explicitly using `match`.
|
|
fn give_adult(drink: Option<&str>) {
|
|
// Specify a course of action for each case.
|
|
match drink {
|
|
Some("lemonade") => println!("Yuck! Too sugary."),
|
|
Some(inner) => println!("{}? How nice.", inner),
|
|
None => println!("No drink? Oh well."),
|
|
}
|
|
}
|
|
|
|
// Others will `panic` before drinking sugary drinks.
|
|
// All drinks are handled implicitly using `unwrap`.
|
|
fn drink(drink: Option<&str>) {
|
|
// `unwrap` returns a `panic` when it receives a `None`.
|
|
let inside = drink.unwrap();
|
|
if inside == "lemonade" { panic!("AAAaaaaa!!!!"); }
|
|
|
|
println!("I love {}s!!!!!", inside);
|
|
}
|
|
|
|
fn part0() {
|
|
let water = Some("water");
|
|
let lemonade = Some("lemonade");
|
|
let void = None;
|
|
|
|
give_adult(water);
|
|
give_adult(lemonade);
|
|
give_adult(void);
|
|
|
|
let coffee = Some("coffee");
|
|
let nothing = None;
|
|
|
|
drink(coffee);
|
|
drink(nothing);
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
}
|
|
|