1
00:00:00,420 --> 00:00:06,689
In this lecture, we are going to talk about one final concept before moving on to WebAssembly.

2
00:00:06,990 --> 00:00:08,940
I know you're excited to get started.

3
00:00:09,210 --> 00:00:13,320
I promise this is the last topic before working on a real project.

4
00:00:13,620 --> 00:00:16,800
There's a special type in rust called results.

5
00:00:17,430 --> 00:00:18,870
It's a very common type.

6
00:00:19,260 --> 00:00:23,100
You'll likely encounter this type whenever working on a project.

7
00:00:23,460 --> 00:00:26,160
The idea of the result type is simple.

8
00:00:26,460 --> 00:00:32,070
The result type can store two types of values a valid value or an error.

9
00:00:32,430 --> 00:00:36,810
Just because our code compiles doesn't mean we won't encounter errors.

10
00:00:37,380 --> 00:00:40,350
For example, let's say we need to open a file.

11
00:00:40,590 --> 00:00:42,510
What if the file fails to open?

12
00:00:42,810 --> 00:00:46,710
Here's another scenario we may need to connect to a database.

13
00:00:47,010 --> 00:00:49,950
It's possible the hosting provider may be down.

14
00:00:50,340 --> 00:00:53,940
External forces beyond our control can cause errors.

15
00:00:54,240 --> 00:00:57,750
Rust provides a type for storing errors or values.

16
00:00:58,020 --> 00:00:59,250
Let's give it a try.

17
00:00:59,820 --> 00:01:04,500
Above the main function, let's define a function called is verified.

18
00:01:07,040 --> 00:01:12,320
This function will accept an argument called accounts annotated with the bank account type.

19
00:01:15,060 --> 00:01:21,030
For extra precaution, we should borrow the value by adding the ampersand symbol at the beginning of

20
00:01:21,030 --> 00:01:21,570
the tape.

21
00:01:24,450 --> 00:01:31,350
As the name suggests, this function will check if an account is verified, if they are, we will return

22
00:01:31,350 --> 00:01:35,320
a Boolean value on top of returning, i.e. Boolean value.

23
00:01:35,370 --> 00:01:38,640
We should throw an error if the account is not verified.

24
00:01:39,030 --> 00:01:45,840
There are two possible scenarios either the accounts can be successfully verified or it produces an

25
00:01:45,840 --> 00:01:49,330
error for the return type of this function.

26
00:01:49,350 --> 00:01:51,180
We will send it to results.

27
00:01:53,810 --> 00:01:57,590
We can hover our mouse over this type to learn more information.

28
00:01:57,890 --> 00:02:00,530
An error will appear, but we can ignore it.

29
00:02:00,860 --> 00:02:07,700
Scrolling to the bottom, we're given a description that says the following result is a type that represents

30
00:02:07,700 --> 00:02:09,889
either a success or failure.

31
00:02:10,580 --> 00:02:15,200
The result type can act as a container for more data about the results.

32
00:02:15,590 --> 00:02:21,650
The compiler requires us to annotate the values for successful or failed operations.

33
00:02:22,070 --> 00:02:24,800
We can do so with a feature called generics.

34
00:02:25,100 --> 00:02:31,040
You can think of generics as function arguments, but with types and set of values, they're commonly

35
00:02:31,040 --> 00:02:33,260
used with functions and structures.

36
00:02:35,860 --> 00:02:41,950
Generics can be added to a type by adding a pair of angle brackets inside these brackets.

37
00:02:42,100 --> 00:02:45,700
We can specify the values contained in the result type.

38
00:02:46,120 --> 00:02:49,540
The first type will correspond to a successful results.

39
00:02:49,930 --> 00:02:54,440
The second type will correspond with the error for this demonstration.

40
00:02:54,460 --> 00:02:57,760
We're going to set both types to Boolean types.

41
00:03:00,360 --> 00:03:04,720
We can begin writing the logic for the function inside the function.

42
00:03:04,740 --> 00:03:08,550
We're going to run a match on the account verified property.

43
00:03:11,200 --> 00:03:13,060
Let's handle both scenarios.

44
00:03:13,330 --> 00:03:15,790
The field can either be true or false.

45
00:03:18,310 --> 00:03:23,310
If the field matches with true, we are going to return a function called OK.

46
00:03:26,000 --> 00:03:33,230
We are obligated to return a value with the type of results the OK function will return this type for

47
00:03:33,230 --> 00:03:35,310
us inside this function.

48
00:03:35,330 --> 00:03:38,450
We can add the value for a successful results.

49
00:03:38,840 --> 00:03:46,220
It must correspond to the first type in the generic earlier we specify the result type will contain

50
00:03:46,220 --> 00:03:49,160
a Boolean if the operation was a success.

51
00:03:49,490 --> 00:03:50,930
Let's pass in true.

52
00:03:53,700 --> 00:03:54,930
What about errors?

53
00:03:55,170 --> 00:04:00,360
Turns out there's a function for producing a value with the result type for errors.

54
00:04:00,690 --> 00:04:02,820
Let's return a function called error.

55
00:04:05,560 --> 00:04:11,920
Same as before in our generic, we specify the type for the era value will be a Boolean.

56
00:04:12,220 --> 00:04:13,660
Let's pass in false.

57
00:04:16,180 --> 00:04:20,589
We can begin handling the results, switching back to the main function.

58
00:04:20,800 --> 00:04:23,380
We're going to store the results in a variable.

59
00:04:23,740 --> 00:04:27,490
The name of the variable will be called verification status.

60
00:04:29,630 --> 00:04:34,760
The value will be the is verified function with the my account variable.

61
00:04:37,390 --> 00:04:39,820
Remember to add the ampersand symbol.

62
00:04:40,120 --> 00:04:42,760
The function is expecting to borrow in accounts.

63
00:04:43,060 --> 00:04:47,200
Lastly, let's print this variable with the print line macro.

64
00:04:47,620 --> 00:04:50,020
The results can't be printed directly.

65
00:04:50,230 --> 00:04:52,210
They must be formatted beforehand.

66
00:04:55,710 --> 00:04:56,530
We're finished.

67
00:04:56,730 --> 00:04:58,920
Let's try compiling the project.

68
00:05:01,520 --> 00:05:06,110
In the output, we'll find the return value contained in the result type.

69
00:05:06,470 --> 00:05:13,070
However, it's not entirely ideal in most cases, we're not interested in the result container.

70
00:05:13,460 --> 00:05:15,980
You may want to directly grab the value.

71
00:05:16,220 --> 00:05:18,260
Here's where things get interesting.

72
00:05:18,650 --> 00:05:24,260
Rust offers various solutions for accessing a value from a successful result.

73
00:05:24,650 --> 00:05:30,530
We're not going to be able to explore every option, but let's look at the most common approaches for

74
00:05:30,530 --> 00:05:31,880
retrieving a value.

75
00:05:32,600 --> 00:05:36,830
The first option is to chain a function called unwrap on the results.

76
00:05:39,420 --> 00:05:45,840
The unwrap function is a function you will come across often it'll grab the value from a successful

77
00:05:45,840 --> 00:05:46,440
result.

78
00:05:46,770 --> 00:05:49,200
Let's try compiling the project again.

79
00:05:51,890 --> 00:05:54,380
This time, U-value is retrieved.

80
00:05:54,740 --> 00:05:58,070
What do you think would happen if the result contained an error?

81
00:05:58,460 --> 00:06:02,090
Let's find out inside the my account variable.

82
00:06:02,270 --> 00:06:05,660
I'm going to update the verified field to false.

83
00:06:08,230 --> 00:06:10,630
Next, I'll compile the project.

84
00:06:13,040 --> 00:06:19,550
By default, Russell will panic when an error is thrown by a function, the unwrap function will never

85
00:06:19,550 --> 00:06:20,660
return a value.

86
00:06:20,900 --> 00:06:23,690
As a result, we can never view the results.

87
00:06:24,200 --> 00:06:30,020
The error produced by Russell should give you enough information as to what's going on.

88
00:06:30,800 --> 00:06:34,220
There's an alternative function called the expect function.

89
00:06:34,580 --> 00:06:38,300
Let's replace the unwrap function with the expected function.

90
00:06:40,620 --> 00:06:45,000
The expect function performs a similar task to the unwrap function.

91
00:06:45,360 --> 00:06:52,260
It'll retrieve the value from a successful results, however, it'll allow us to output a custom error

92
00:06:52,260 --> 00:06:54,120
message if an error is given.

93
00:06:54,540 --> 00:06:57,810
We can write the message inside the expect function.

94
00:06:58,140 --> 00:07:00,210
Let's pass in the following message.

95
00:07:00,420 --> 00:07:02,360
Unable to unwrap results.

96
00:07:04,970 --> 00:07:07,760
Time to compile the project one more time.

97
00:07:10,270 --> 00:07:15,820
Russ will output the same error message with our error message mixed in perfect.

98
00:07:16,060 --> 00:07:19,900
We have two functions for grabbing the response from a result type.

99
00:07:20,230 --> 00:07:24,640
Admittedly, this isn't the best example of how to use the result type.

100
00:07:25,150 --> 00:07:28,570
We have complete control over when an error gets produced.

101
00:07:28,900 --> 00:07:33,460
The purpose of the result type is to handle actions that can produce an error.

102
00:07:33,760 --> 00:07:39,940
When we start working on the mini project, we'll come across real examples of how we can use the result

103
00:07:39,940 --> 00:07:40,330
type.

104
00:07:40,930 --> 00:07:45,460
There's one more thing I want to mention in the resource section of this lecture.

105
00:07:45,640 --> 00:07:50,050
I provide a link to a chapter from the official Rust book error.

106
00:07:50,050 --> 00:07:52,930
Handling is a big subject in the Rust community.

107
00:07:53,230 --> 00:07:57,100
There are various opinions and approaches to handling errors.

108
00:07:57,400 --> 00:08:03,490
It's not as simple as it may seem a good starting point for learning more as this chapter from this

109
00:08:03,490 --> 00:08:03,880
book.

110
00:08:04,210 --> 00:08:06,360
I highly recommend giving it a read.

111
00:08:06,370 --> 00:08:13,180
After finishing this mini course in the next lecture, we are going to begin developing a real web assembly

112
00:08:13,180 --> 00:08:13,870
project.

113
00:08:14,140 --> 00:08:15,070
I hope you're ready.

114
00:08:15,280 --> 00:08:17,620
It's going to be a great learning experience.

115
00:08:17,860 --> 00:08:18,850
I'll see you there.

