| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | AA | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | Which Option<T> method should I use? | ||||||||||||||||||||||||||
2 | |||||||||||||||||||||||||||
3 | Rust's Option type has a bunch of methods that exist solely to help you get the value you need, when you've already got an Option value that is almost what you need. But which method should you use? | ||||||||||||||||||||||||||
4 | |||||||||||||||||||||||||||
5 | what happens if self == Some(x) | ||||||||||||||||||||||||||
6 | x ("unwrap") | self | Some(g(x)) | g(x) | u | ||||||||||||||||||||||
7 | what happens if self == None | None ("and") | (type mismatch) | (identity) | .map(g) | .and_then(g) | .and(u) | ||||||||||||||||||||
8 | v ("or") | .unwrap_or(v) | .or(v) | .map_or(v, g) | |||||||||||||||||||||||
9 | f() ("or_else") | .unwrap_or_else(f) | .or_else(f) | .map_or_else(f, g) | |||||||||||||||||||||||
10 | panic! | .unwrap() .expect(msg) | |||||||||||||||||||||||||
11 | |||||||||||||||||||||||||||
12 | I made this table because I can never remember which method is which. :) But I'm not sure this is a good way to present it. The exercise was to figure out what those terms in blue mean ("and" etc.) but I'm not sure I know any more than when I started out. :) | ||||||||||||||||||||||||||
13 | |||||||||||||||||||||||||||
14 | Note that "map" doesn't seem to have a consistent meaning. "then" only appears once. | ||||||||||||||||||||||||||
15 | |||||||||||||||||||||||||||
16 | In case you do monads: .and_then(g) is the equivalent of Haskell's monadic bind operator (>>= g), and .and(u) is (>> u). | ||||||||||||||||||||||||||
17 | |||||||||||||||||||||||||||
18 | Contributors: talchas (thanks!) | ||||||||||||||||||||||||||
19 | Discussion: https://users.rust-lang.org/t/the-many-methods-of-option/5906 | ||||||||||||||||||||||||||
20 | |||||||||||||||||||||||||||
21 | |||||||||||||||||||||||||||
22 | |||||||||||||||||||||||||||
23 | |||||||||||||||||||||||||||
24 | |||||||||||||||||||||||||||
25 | |||||||||||||||||||||||||||
26 | |||||||||||||||||||||||||||
27 | |||||||||||||||||||||||||||
28 | |||||||||||||||||||||||||||
29 | |||||||||||||||||||||||||||
30 | |||||||||||||||||||||||||||
31 | |||||||||||||||||||||||||||
32 | |||||||||||||||||||||||||||
33 | |||||||||||||||||||||||||||
34 | |||||||||||||||||||||||||||
35 | |||||||||||||||||||||||||||
36 | |||||||||||||||||||||||||||
37 | |||||||||||||||||||||||||||
38 | |||||||||||||||||||||||||||
39 | |||||||||||||||||||||||||||
40 | |||||||||||||||||||||||||||
41 | |||||||||||||||||||||||||||
42 | |||||||||||||||||||||||||||
43 | |||||||||||||||||||||||||||
44 | |||||||||||||||||||||||||||
45 | |||||||||||||||||||||||||||
46 | |||||||||||||||||||||||||||
47 | |||||||||||||||||||||||||||
48 | |||||||||||||||||||||||||||
49 | |||||||||||||||||||||||||||
50 | |||||||||||||||||||||||||||
51 | |||||||||||||||||||||||||||
52 | |||||||||||||||||||||||||||
53 | |||||||||||||||||||||||||||
54 | |||||||||||||||||||||||||||
55 | |||||||||||||||||||||||||||
56 | |||||||||||||||||||||||||||
57 | |||||||||||||||||||||||||||
58 | |||||||||||||||||||||||||||
59 | |||||||||||||||||||||||||||
60 | |||||||||||||||||||||||||||
61 | |||||||||||||||||||||||||||
62 | |||||||||||||||||||||||||||
63 | |||||||||||||||||||||||||||
64 | |||||||||||||||||||||||||||
65 | |||||||||||||||||||||||||||
66 | |||||||||||||||||||||||||||
67 | |||||||||||||||||||||||||||
68 | |||||||||||||||||||||||||||
69 | |||||||||||||||||||||||||||
70 | |||||||||||||||||||||||||||
71 | |||||||||||||||||||||||||||
72 | |||||||||||||||||||||||||||
73 | |||||||||||||||||||||||||||
74 | |||||||||||||||||||||||||||
75 | |||||||||||||||||||||||||||
76 | |||||||||||||||||||||||||||
77 | |||||||||||||||||||||||||||
78 | |||||||||||||||||||||||||||
79 | |||||||||||||||||||||||||||
80 | |||||||||||||||||||||||||||
81 | |||||||||||||||||||||||||||
82 | |||||||||||||||||||||||||||
83 | |||||||||||||||||||||||||||
84 | |||||||||||||||||||||||||||
85 | |||||||||||||||||||||||||||
86 | |||||||||||||||||||||||||||
87 | |||||||||||||||||||||||||||
88 | |||||||||||||||||||||||||||
89 | |||||||||||||||||||||||||||
90 | |||||||||||||||||||||||||||
91 | |||||||||||||||||||||||||||
92 | |||||||||||||||||||||||||||
93 | |||||||||||||||||||||||||||
94 | |||||||||||||||||||||||||||
95 | |||||||||||||||||||||||||||
96 | |||||||||||||||||||||||||||
97 | |||||||||||||||||||||||||||
98 | |||||||||||||||||||||||||||
99 | |||||||||||||||||||||||||||
100 | |||||||||||||||||||||||||||