1
00:00:00,150 --> 00:00:01,110
Welcome to module 8,
2
00:00:01,860 --> 00:00:02,690
Useful methods on
3
00:00:02,930 --> 00:00:04,250
result and option.
4
00:00:05,750 --> 00:00:06,590
In this module,
5
00:00:06,980 --> 00:00:08,900
we're going to cover the most useful methods
6
00:00:09,100 --> 00:00:10,729
the Standard Library has defined on
7
00:00:10,929 --> 00:00:11,780
Result and Option.
8
00:00:12,290 --> 00:00:14,480
We'll see how to test for which variant we have,
9
00:00:14,930 --> 00:00:16,910
how to convert a Result to an Option
10
00:00:17,110 --> 00:00:17,780
and vice versa,
11
00:00:18,480 --> 00:00:20,420
how to specify a fallback value
12
00:00:20,620 --> 00:00:21,630
to use in the error
13
00:00:21,830 --> 00:00:22,740
and None cases,
14
00:00:23,330 --> 00:00:24,830
and how to transform the Ok
15
00:00:25,030 --> 00:00:26,060
or Some values further,
16
00:00:26,510 --> 00:00:28,399
but leave error or None values as they
17
00:00:28,599 --> 00:00:28,700
are.
18
00:00:30,240 --> 00:00:32,309
Let's start with testing to see which variant
19
00:00:32,509 --> 00:00:32,820
we have.
20
00:00:34,320 --> 00:00:36,090
The names of these methods are different
21
00:00:36,290 --> 00:00:37,170
for the two types.
22
00:00:37,650 --> 00:00:39,840
Result has the methods is_ok
23
00:00:40,110 --> 00:00:41,999
and is_err for testing
24
00:00:42,199 --> 00:00:43,740
whether a value of type Result
25
00:00:44,070 --> 00:00:46,209
is the Ok variant or the Err
26
00:00:46,409 --> 00:00:46,609
variant.
27
00:00:47,270 --> 00:00:49,490
Option has the methods is_some
28
00:00:49,730 --> 00:00:51,709
and is_none that test whether
29
00:00:51,909 --> 00:00:53,240
a value is the Some variant
30
00:00:53,450 --> 00:00:54,350
or the None variant.
31
00:00:55,870 --> 00:00:57,759
These methods are useful when you want
32
00:00:57,959 --> 00:00:59,450
to know which variant you have,
33
00:00:59,890 --> 00:01:01,720
but either don't want to or can't
34
00:01:01,920 --> 00:01:03,759
test what the values within the variants
35
00:01:03,959 --> 00:01:04,030
are.
36
00:01:04,930 --> 00:01:06,910
These tests could be asserts
37
00:01:07,110 --> 00:01:08,260
in automated tests
38
00:01:08,770 --> 00:01:10,930
or in conditionals used for control flow.
39
00:01:11,890 --> 00:01:13,749
These methods shouldn't be used in
40
00:01:13,949 --> 00:01:14,589
combination
41
00:01:14,789 --> 00:01:15,430
with unwrap -
42
00:01:16,030 --> 00:01:18,070
the method that returns the inner value
43
00:01:18,270 --> 00:01:19,330
of an Ok or Some
44
00:01:19,610 --> 00:01:20,980
and panics otherwise.
45
00:01:22,470 --> 00:01:24,350
An example of this antipattern
46
00:01:24,860 --> 00:01:26,840
is checking if a value is some
47
00:01:27,340 --> 00:01:28,700
and unwrapping it if so.
48
00:01:30,270 --> 00:01:32,460
Yes, this ensures the unwrap
49
00:01:32,660 --> 00:01:33,720
won't cause a panic,
50
00:01:34,260 --> 00:01:35,880
but the code isn't very clear.
51
00:01:36,720 --> 00:01:38,639
You'd have to read this code carefully to
52
00:01:38,839 --> 00:01:40,080
notice that it won't panic.
53
00:01:41,060 --> 00:01:43,310
The code is also doing a redundant check
54
00:01:43,550 --> 00:01:45,290
of which variant the value is.
55
00:01:46,810 --> 00:01:49,180
A better choice with regards to code readability
56
00:01:49,380 --> 00:01:51,039
and runtime performance would be to
57
00:01:51,239 --> 00:01:52,570
use match, if let,
58
00:01:53,140 --> 00:01:55,029
or one of the methods we'll be covering in the
59
00:01:55,229 --> 00:01:55,890
rest of the module.
60
00:01:57,380 --> 00:01:57,950
Next,
61
00:01:58,310 --> 00:02:00,440
let's talk about converting between Result
62
00:02:00,640 --> 00:02:01,100
and Option.
63
00:02:02,580 --> 00:02:04,680
To convert a Result into an Option,
64
00:02:05,160 --> 00:02:06,630
you can use the ok method.
65
00:02:07,610 --> 00:02:09,110
If you call ok on an
66
00:02:09,310 --> 00:02:11,029
Ok value, you'll get
67
00:02:11,229 --> 00:02:12,979
a Some variant holding the same
68
00:02:13,179 --> 00:02:14,790
value that the Ok was holding.
69
00:02:15,790 --> 00:02:17,649
If you call ok on an Err
70
00:02:17,849 --> 00:02:18,160
value,
71
00:02:18,610 --> 00:02:19,870
you'll get a None variant,
72
00:02:20,350 --> 00:02:22,209
and the value inside the error will be
73
00:02:22,409 --> 00:02:22,820
thrown away.
74
00:02:24,330 --> 00:02:26,400
To convert an Option into a Result,
75
00:02:27,000 --> 00:02:28,080
you can use the ok_or
76
00:02:28,280 --> 00:02:28,770
method.
77
00:02:29,750 --> 00:02:30,739
If you call ok_or
78
00:02:30,939 --> 00:02:32,020
on a Some value,
79
00:02:32,740 --> 00:02:34,819
you'll get an Ok variant holding the same
80
00:02:35,019 --> 00:02:36,350
value that the Some was holding.
81
00:02:37,350 --> 00:02:39,360
When we converted from Result to Option,
82
00:02:39,780 --> 00:02:41,370
we lost the error information,
83
00:02:41,850 --> 00:02:44,030
which means to convert from Option to Result,
84
00:02:44,520 --> 00:02:45,960
we need to add information -
85
00:02:46,440 --> 00:02:48,479
the error value that None should correspond
86
00:02:48,679 --> 00:02:48,780
to.
87
00:02:50,270 --> 00:02:51,370
That's what the argument to
88
00:02:51,570 --> 00:02:52,660
ok_or is.
89
00:02:53,360 --> 00:02:55,380
If the value is None, calling
90
00:02:55,580 --> 00:02:57,490
ok_or will return the argument
91
00:02:57,690 --> 00:02:58,820
inside an Err variant.
92
00:03:00,300 --> 00:03:02,580
Next, let's talk about specifying
93
00:03:02,780 --> 00:03:04,530
fallback values by using the
94
00:03:04,730 --> 00:03:05,720
unwrap_or method.
95
00:03:07,240 --> 00:03:08,410
If called on Ok
96
00:03:08,610 --> 00:03:09,010
or Some,
97
00:03:09,640 --> 00:03:11,499
unwrap_or extracts the value
98
00:03:11,699 --> 00:03:12,270
from the variant,
99
00:03:12,760 --> 00:03:13,510
and returns that
100
00:03:13,710 --> 00:03:14,400
so you can use it.
101
00:03:15,380 --> 00:03:16,430
If called on Err
102
00:03:16,630 --> 00:03:17,060
or None,
103
00:03:17,480 --> 00:03:19,469
unwrap_or returns the fallback
104
00:03:19,669 --> 00:03:21,300
value specified as an argument.
105
00:03:22,780 --> 00:03:24,759
For example, in the module where
106
00:03:24,959 --> 00:03:26,590
we introduced Result and Option,
107
00:03:27,250 --> 00:03:28,899
we tried to parse JSON that may
108
00:03:29,099 --> 00:03:29,710
or may not be valid.
109
00:03:30,700 --> 00:03:32,890
We used the match expression to return
110
00:03:33,090 --> 00:03:34,250
a fallback Person value,
111
00:03:34,450 --> 00:03:35,260
if there is an error.
112
00:03:36,220 --> 00:03:37,240
Instead of the match,
113
00:03:37,660 --> 00:03:38,679
we can use unwrap_or
114
00:03:38,879 --> 00:03:40,619
and specify the fallback
115
00:03:40,819 --> 00:03:41,050
value.
116
00:03:42,550 --> 00:03:44,380
We can shorten this code further
117
00:03:44,590 --> 00:03:45,700
using the
118
00:03:45,900 --> 00:03:46,830
unwrap_or_default method.
119
00:03:47,840 --> 00:03:49,560
This method doesn't take an argument
120
00:03:50,000 --> 00:03:51,320
but always uses the type's
121
00:03:51,520 --> 00:03:52,130
default value.
122
00:03:53,120 --> 00:03:55,189
It makes use of the functionality defined
123
00:03:55,389 --> 00:03:56,230
by the Default trait
124
00:03:56,750 --> 00:03:58,729
and otherwise has the same behaviour as
125
00:03:58,929 --> 00:03:59,540
unwrap_or.
126
00:04:01,060 --> 00:04:03,000
So, if the type inside the Ok
127
00:04:03,700 --> 00:04:05,650
derives or implements the Default trait,
128
00:04:06,660 --> 00:04:07,799
you can use the
129
00:04:07,999 --> 00:04:08,640
unwrap_or_default method.
130
00:04:10,120 --> 00:04:12,190
Next, let's talk about a method
131
00:04:12,390 --> 00:04:13,840
that lets us transform Ok
132
00:04:14,040 --> 00:04:15,100
or Some values further.
133
00:04:16,560 --> 00:04:17,970
This method is called map.
134
00:04:18,910 --> 00:04:20,830
It's useful when you want to perform an
135
00:04:21,030 --> 00:04:22,720
operation on the value in
136
00:04:22,920 --> 00:04:23,950
Ok or Some.
137
00:04:24,670 --> 00:04:25,540
But leave Err
138
00:04:25,740 --> 00:04:27,310
or None values as they are.
139
00:04:28,630 --> 00:04:30,300
Here's a simplified example
140
00:04:30,510 --> 00:04:31,770
from crates.io code.
141
00:04:32,220 --> 00:04:34,109
We have a Version struct that may
142
00:04:34,309 --> 00:04:36,029
or may not have the id of the
143
00:04:36,229 --> 00:04:38,100
user who published that crate version
144
00:04:38,490 --> 00:04:40,600
because we only recently started tracking
145
00:04:40,800 --> 00:04:41,460
that information.
146
00:04:42,440 --> 00:04:44,449
To get the User struct that goes
147
00:04:44,649 --> 00:04:46,250
with the user ID in the version,
148
00:04:46,500 --> 00:04:48,650
we've defined the published_by method
149
00:04:48,960 --> 00:04:51,030
that has the return type Option.
150
00:04:52,520 --> 00:04:54,510
If the version's published_by attribute
151
00:04:54,710 --> 00:04:55,620
is the Some variant,
152
00:04:56,040 --> 00:04:56,970
meaning it has an ID,
153
00:04:57,780 --> 00:04:59,700
we use map to look up the user
154
00:04:59,900 --> 00:05:00,620
in the database.
155
00:05:01,580 --> 00:05:03,030
If published_by is None,
156
00:05:03,480 --> 00:05:04,590
the map does nothing
157
00:05:04,790 --> 00:05:06,240
and passes the None value through.
158
00:05:07,170 --> 00:05:09,209
We've provided simplified definitions
159
00:05:09,409 --> 00:05:10,200
for the User struct
160
00:05:10,400 --> 00:05:11,580
and the user find function
161
00:05:12,050 --> 00:05:13,860
so that we can run this main function
162
00:05:14,060 --> 00:05:15,749
that creates a version that has the
163
00:05:15,949 --> 00:05:16,350
user id
164
00:05:16,720 --> 00:05:17,850
and a version that doesn't.
165
00:05:19,390 --> 00:05:20,470
When we run this code,
166
00:05:20,860 --> 00:05:22,660
we see the result of calling map
167
00:05:22,860 --> 00:05:24,500
is Some(User struct instance
168
00:05:24,700 --> 00:05:25,780
if published by is Some,
169
00:05:26,470 --> 00:05:28,450
and None if published by is None.
170
00:05:29,920 --> 00:05:31,739
There are many more methods defined on
171
00:05:31,939 --> 00:05:33,579
Result and Option than we can
172
00:05:33,779 --> 00:05:34,740
cover in this video.
173
00:05:35,750 --> 00:05:37,800
Lots are slight variations on the ones
174
00:05:38,000 --> 00:05:38,840
we have touched on,
175
00:05:39,200 --> 00:05:41,089
such as methods that take a closure to
176
00:05:41,289 --> 00:05:41,600
apply,
177
00:05:41,990 --> 00:05:43,040
rather than a value
178
00:05:43,240 --> 00:05:44,090
or vice versa.
179
00:05:45,060 --> 00:05:47,010
By learning about some of these methods,
180
00:05:47,210 --> 00:05:49,259
you now have a good idea of what many
181
00:05:49,459 --> 00:05:49,890
of them do.
182
00:05:51,370 --> 00:05:53,410
Overall, if you find yourself writing
183
00:05:53,610 --> 00:05:55,720
a match expression to handle a Result
184
00:05:55,920 --> 00:05:56,440
or Option,
185
00:05:56,970 --> 00:05:59,030
and the match feels like it's a common operation
186
00:05:59,230 --> 00:06:00,340
many people would want to do,
187
00:06:00,880 --> 00:06:03,100
check the documentation to see if there is a method
188
00:06:03,300 --> 00:06:04,600
that does what you're doing by hand.
189
00:06:06,070 --> 00:06:06,850
In this module,
190
00:06:07,150 --> 00:06:09,120
we discussed a bunch of useful methods on
191
00:06:09,320 --> 00:06:10,510
Result and Option.
192
00:06:11,490 --> 00:06:13,350
We covered how to test which variant you
193
00:06:13,550 --> 00:06:13,710
have,
194
00:06:14,160 --> 00:06:14,880
with is_ok,
195
00:06:15,650 --> 00:06:17,400
is_err, is_some,
196
00:06:17,810 --> 00:06:18,630
and is_none.
197
00:06:19,620 --> 00:06:21,900
We showed how to convert results to options
198
00:06:22,100 --> 00:06:22,550
with ok,
199
00:06:22,970 --> 00:06:24,300
and options for results
200
00:06:24,500 --> 00:06:25,190
with okay_or.
201
00:06:26,810 --> 00:06:28,880
We talked about specifying fallback values
202
00:06:29,080 --> 00:06:29,660
with unwrap_or
203
00:06:30,210 --> 00:06:31,310
and unwrap_or_default.
204
00:06:32,780 --> 00:06:34,880
And we looked at transforming values within
205
00:06:35,080 --> 00:06:35,930
Ok or Some
206
00:06:36,440 --> 00:06:37,630
using the map method.
207
00:06:39,190 --> 00:06:40,990
That's the end of the Error Handling unit.
208
00:06:41,410 --> 00:06:41,990
Next unit,
209
00:06:42,270 --> 00:06:44,280
we'll learn all about lifetimes.