11# ` Option ` & ` unwrap `
22
33In the last example, we showed that we can induce program failure at will.
4- We told our program to ` panic ` if the royal received an inappropriate
5- gift - a snake. But what if the royal expected a gift and didn 't receive
6- one? That case would be just as bad, so it needs to be handled!
4+ We told our program to ` panic ` if we drink a sugary lemonade.
5+ But what if we expect _ some _ drink but don 't receive one?
6+ That case would be just as bad, so it needs to be handled!
77
8- We * could* test this against the null string (` "" ` ) as we do with a snake .
8+ We * could* test this against the null string (` "" ` ) as we do with a lemonade .
99Since we're using Rust, let's instead have the compiler point out cases
10- where there's no gift .
10+ where there's no drink .
1111
1212An ` enum ` called ` Option<T> ` in the ` std ` library is used when absence is a
1313possibility. It manifests itself as one of two "options":
@@ -24,41 +24,41 @@ handling. In the following example, explicit handling yields a more
2424controlled result while retaining the option to ` panic ` if desired.
2525
2626``` rust,editable,ignore,mdbook-runnable
27- // The commoner has seen it all, and can handle any gift well.
28- // All gifts are handled explicitly using `match`.
29- fn give_commoner(gift : Option<&str>) {
27+ // The adult has seen it all, and can handle any drink well.
28+ // All drinks are handled explicitly using `match`.
29+ fn give_adult(drink : Option<&str>) {
3030 // Specify a course of action for each case.
31- match gift {
32- Some("snake ") => println!("Yuck! I'm putting this snake back in the forest ."),
31+ match drink {
32+ Some("lemonade ") => println!("Yuck! Too sugary ."),
3333 Some(inner) => println!("{}? How nice.", inner),
34- None => println!("No gift ? Oh well."),
34+ None => println!("No drink ? Oh well."),
3535 }
3636}
3737
38- // Our sheltered royal will `panic` at the sight of snakes .
39- // All gifts are handled implicitly using `unwrap`.
40- fn give_royal(gift : Option<&str>) {
38+ // Others will `panic` before drinking sugary drinks .
39+ // All drinks are handled implicitly using `unwrap`.
40+ fn drink(drink : Option<&str>) {
4141 // `unwrap` returns a `panic` when it receives a `None`.
42- let inside = gift .unwrap();
43- if inside == "snake " { panic!("AAAaaaaa!!!!"); }
42+ let inside = drink .unwrap();
43+ if inside == "lemonade " { panic!("AAAaaaaa!!!!"); }
4444
4545 println!("I love {}s!!!!!", inside);
4646}
4747
4848fn main() {
49- let food = Some("cabbage ");
50- let snake = Some("snake ");
49+ let water = Some("water ");
50+ let lemonade = Some("lemonade ");
5151 let void = None;
5252
53- give_commoner(food );
54- give_commoner(snake );
55- give_commoner (void);
53+ give_adult(water );
54+ give_adult(lemonade );
55+ give_adult (void);
5656
57- let bird = Some("robin ");
57+ let coffee = Some("coffee ");
5858 let nothing = None;
5959
60- give_royal(bird );
61- give_royal (nothing);
60+ drink(coffee );
61+ drink (nothing);
6262}
6363```
6464
0 commit comments