1
1
# ` Option ` & ` unwrap `
2
2
3
3
In 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!
7
7
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 .
9
9
Since 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 .
11
11
12
12
An ` enum ` called ` Option<T> ` in the ` std ` library is used when absence is a
13
13
possibility. It manifests itself as one of two "options":
@@ -24,41 +24,41 @@ handling. In the following example, explicit handling yields a more
24
24
controlled result while retaining the option to ` panic ` if desired.
25
25
26
26
``` 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>) {
30
30
// 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 ."),
33
33
Some(inner) => println!("{}? How nice.", inner),
34
- None => println!("No gift ? Oh well."),
34
+ None => println!("No drink ? Oh well."),
35
35
}
36
36
}
37
37
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>) {
41
41
// `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!!!!"); }
44
44
45
45
println!("I love {}s!!!!!", inside);
46
46
}
47
47
48
48
fn main() {
49
- let food = Some("cabbage ");
50
- let snake = Some("snake ");
49
+ let water = Some("water ");
50
+ let lemonade = Some("lemonade ");
51
51
let void = None;
52
52
53
- give_commoner(food );
54
- give_commoner(snake );
55
- give_commoner (void);
53
+ give_adult(water );
54
+ give_adult(lemonade );
55
+ give_adult (void);
56
56
57
- let bird = Some("robin ");
57
+ let coffee = Some("coffee ");
58
58
let nothing = None;
59
59
60
- give_royal(bird );
61
- give_royal (nothing);
60
+ drink(coffee );
61
+ drink (nothing);
62
62
}
63
63
```
64
64
0 commit comments