1 00:00:00,000 --> 00:00:07,933 [No Audio] 2 00:00:07,934 --> 00:00:10,300 In Rust programming, there are a couple of 3 00:00:10,301 --> 00:00:12,500 enums that are very frequently used, and they 4 00:00:12,501 --> 00:00:15,500 have a specific use in certain circumstances. 5 00:00:15,666 --> 00:00:17,800 Their understanding is essential for becoming 6 00:00:17,801 --> 00:00:20,200 proficient in Rust programming. These two 7 00:00:20,201 --> 00:00:23,133 enums are the option and result enums. We will 8 00:00:23,134 --> 00:00:25,233 cover the option enum in this tutorial, and 9 00:00:25,234 --> 00:00:27,466 the result in them in the later on tutorial. 10 00:00:27,600 --> 00:00:29,366 Let's start learning the option enum. 11 00:00:30,066 --> 00:00:33,033 Sometimes it's, it's desirable to capture the 12 00:00:33,034 --> 00:00:35,700 failure of some segment or part of a program. 13 00:00:35,966 --> 00:00:37,800 This is typically accomplished using the 14 00:00:37,801 --> 00:00:40,866 option enum in Rust. As discussed, each enum 15 00:00:40,867 --> 00:00:43,566 type is associated with some variants. So the 16 00:00:43,567 --> 00:00:45,833 first thing to note about the option enum is 17 00:00:45,834 --> 00:00:48,600 that, it has two variants. The first variant 18 00:00:48,601 --> 00:00:51,066 is called None, which is used to indicate 19 00:00:51,067 --> 00:00:53,466 failure or lack of value. And the second 20 00:00:53,467 --> 00:00:57,233 variant is called Some, which is basically a 21 00:00:57,234 --> 00:01:00,400 tuple struct that wraps a value with type T, 22 00:01:00,401 --> 00:01:02,666 where T is a generic type. Due to its 23 00:01:02,667 --> 00:01:05,400 importance, the option enum are included in 24 00:01:05,401 --> 00:01:08,466 the programs by default. Before using the 25 00:01:08,467 --> 00:01:11,866 option enum, we'll look at how it is defined in Rust. 26 00:01:13,466 --> 00:01:15,300 You may note that, it uses the 27 00:01:15,301 --> 00:01:17,866 generic type T which can be any type, and 28 00:01:17,867 --> 00:01:21,033 there are two variants None and Some, the None 29 00:01:21,034 --> 00:01:24,000 variant is used to indicate a failure or lack 30 00:01:24,001 --> 00:01:26,466 of a value, while the Some variant indicates 31 00:01:26,467 --> 00:01:29,033 some value having any type. Let us look at a 32 00:01:29,034 --> 00:01:31,133 simple example to see the basic use of the 33 00:01:31,134 --> 00:01:34,566 option enum. We will first initialize a 34 00:01:34,567 --> 00:01:37,066 variable of type Option which will have None 35 00:01:37,067 --> 00:01:41,766 value. This syntax means that, the disease is 36 00:01:41,800 --> 00:01:44,633 of type Option, which may have an associated 37 00:01:44,634 --> 00:01:47,733 string value, in case of it Some variant. But 38 00:01:47,734 --> 00:01:49,866 by default, it will have a null value which 39 00:01:49,867 --> 00:01:53,600 means no disease by default. Next, we will 40 00:01:53,601 --> 00:01:55,500 assume that for instance, a doctor has made 41 00:01:55,501 --> 00:01:57,933 some investigations, and therefore he is able 42 00:01:57,934 --> 00:01:59,933 to come to the conclusion that, the patient in 43 00:01:59,934 --> 00:02:01,900 question has a disease of diabetes. So 44 00:02:01,901 --> 00:02:04,233 therefore, the disease variable needs to be 45 00:02:04,234 --> 00:02:07,066 updated with the proper disease value. So let 46 00:02:07,067 --> 00:02:09,032 us update the disease variable from Some 47 00:02:09,033 --> 00:02:11,233 variant and inside the Some, the name of the 48 00:02:11,234 --> 00:02:12,766 disease in string format. 49 00:02:12,767 --> 00:02:17,800 [No Audio] 50 00:02:17,801 --> 00:02:20,233 This means now, that the disease has some 51 00:02:20,234 --> 00:02:23,366 value, and is of Option enum type Some. 52 00:02:23,367 --> 00:02:26,133 To check if the variant is Some or None for the 53 00:02:26,134 --> 00:02:28,066 disease variable, we will use a match 54 00:02:28,067 --> 00:02:31,133 statement, we will do a match on the disease. 55 00:02:31,134 --> 00:02:34,900 [No Audio] 56 00:02:34,901 --> 00:02:38,000 Next, we will include variants as arms of the 57 00:02:38,001 --> 00:02:41,500 match. First, we will have an arm for the Some variant. 58 00:02:41,501 --> 00:02:43,966 [No Audio] 59 00:02:43,967 --> 00:02:46,666 Please note that, whatever variable 60 00:02:46,667 --> 00:02:49,000 we mentioned inside the Some, it will be used 61 00:02:49,001 --> 00:02:51,300 to capture the value of the disease. We do 62 00:02:51,301 --> 00:02:53,500 not need to separately initialize a variable 63 00:02:53,501 --> 00:02:55,333 of disease_name, it will be 64 00:02:55,334 --> 00:02:57,233 automatically initialized from the value of 65 00:02:57,234 --> 00:02:58,966 the disease that is wrapped by the Some 66 00:02:58,967 --> 00:03:02,533 variant. Next I will add the arm for the None variant. 67 00:03:02,534 --> 00:03:06,200 [No Audio] 68 00:03:06,201 --> 00:03:07,833 Let us execute now. 69 00:03:07,834 --> 00:03:11,400 [No Audio] 70 00:03:11,401 --> 00:03:13,766 The print statement indicates, in this case, that the 71 00:03:13,767 --> 00:03:16,233 disease is that of diabetes. One final note 72 00:03:16,234 --> 00:03:18,300 before we move to the next example is, that of 73 00:03:18,333 --> 00:03:20,700 the variable of disease_name is 74 00:03:20,701 --> 00:03:22,766 limited in scope to the body of the match, and 75 00:03:22,767 --> 00:03:24,566 will not be known outside the body of the 76 00:03:24,567 --> 00:03:26,833 match. As pointed out in the previous 77 00:03:26,834 --> 00:03:28,800 section, the variable scope is limited to the 78 00:03:28,801 --> 00:03:31,566 curly brackets in which it lies. The option 79 00:03:31,567 --> 00:03:33,500 enum can be used to check for any type of 80 00:03:33,501 --> 00:03:36,000 data. Let us cover this using some suitable 81 00:03:36,001 --> 00:03:39,433 examples. First, we will look at the string 82 00:03:39,434 --> 00:03:41,833 slice type, I will declare a variable of 83 00:03:41,834 --> 00:03:44,266 option type with string slice value. 84 00:03:44,267 --> 00:03:47,966 [No Audio] 85 00:03:47,967 --> 00:03:50,600 To extract the value out of the Some variant, we 86 00:03:50,601 --> 00:03:53,333 will use the unwrap function, let us unwrap 87 00:03:53,334 --> 00:03:55,533 the value of s1 in a print statement. 88 00:03:55,534 --> 00:04:00,300 [No Audio] 89 00:04:00,301 --> 00:04:01,533 Let us execute now. 90 00:04:01,534 --> 00:04:07,466 [No Audio] 91 00:04:07,467 --> 00:04:09,500 The first value is, the Some variant having 92 00:04:09,501 --> 00:04:11,766 the value of string slice encapsulated or 93 00:04:11,767 --> 00:04:14,000 wrapped inside it, and the second value is the 94 00:04:14,001 --> 00:04:16,800 actual value of the string slice. This unwrap 95 00:04:16,801 --> 00:04:18,666 function will be useful for obtaining the 96 00:04:18,667 --> 00:04:21,200 value inside the Some variant. Let us too, have 97 00:04:21,201 --> 00:04:24,300 an example on a numeric values also. Consider 98 00:04:24,301 --> 00:04:27,066 a variable f1 having an option f64 value 99 00:04:27,067 --> 00:04:29,733 initialized from some value of 10.54. 100 00:04:29,734 --> 00:04:33,333 [No Audio] 101 00:04:33,334 --> 00:04:36,300 We can perform operations on the value by unwrapping 102 00:04:36,301 --> 00:04:38,100 the value. For instance, I want to 103 00:04:38,101 --> 00:04:40,433 add it to some other variable, let's say f2 104 00:04:40,434 --> 00:04:43,700 with a value of 16.5, then I will first unwrap 105 00:04:43,701 --> 00:04:45,166 and then perform the addition. 106 00:04:45,167 --> 00:04:49,000 [No Audio] 107 00:04:49,001 --> 00:04:52,100 This will add the two values together. Please note that, 108 00:04:52,101 --> 00:04:54,200 if I change the value of f1 to that of None 109 00:04:54,201 --> 00:04:56,066 variant, then in that case, there will be an 110 00:04:56,067 --> 00:04:58,433 error message, calling the unwrap on a None 111 00:04:58,434 --> 00:05:00,466 value will cause the program to panic, 112 00:05:00,467 --> 00:05:01,633 let us change back. 113 00:05:01,634 --> 00:05:04,133 [No Audio] 114 00:05:04,134 --> 00:05:07,100 Let us add a print statement and print the result of addition 115 00:05:07,101 --> 00:05:09,733 to confirm that the values are being added together. 116 00:05:09,734 --> 00:05:15,666 [No Audio] 117 00:05:15,667 --> 00:05:16,900 Let us cargo run. 118 00:05:16,901 --> 00:05:21,300 [No Audio] 119 00:05:21,301 --> 00:05:23,133 Let us consider some other data types, 120 00:05:23,134 --> 00:05:25,066 such as vectors, the syntax will be 121 00:05:25,100 --> 00:05:27,133 exactly the same as we just learned. For 122 00:05:27,134 --> 00:05:29,433 instance, I will declare an optional vector 123 00:05:29,434 --> 00:05:32,818 of i32, and we'll initialize it from Some values. 124 00:05:32,819 --> 00:05:40,233 [No Audio] 125 00:05:40,234 --> 00:05:42,966 Let us finally learn the use of structures in 126 00:05:42,967 --> 00:05:45,533 connection with the option enum, I will define 127 00:05:45,534 --> 00:05:47,233 a simple Person struct. 128 00:05:47,234 --> 00:05:54,266 [No Audio] 129 00:05:54,267 --> 00:05:56,633 Let us have some instances of the structure. 130 00:05:56,634 --> 00:06:05,500 [No Audio] 131 00:06:05,501 --> 00:06:07,433 Next, I will create a variable of type 132 00:06:07,434 --> 00:06:09,900 optional person, and we'll initialize it from 133 00:06:09,901 --> 00:06:11,333 Some Person p1. 134 00:06:11,334 --> 00:06:17,766 [No Audio] 135 00:06:17,767 --> 00:06:18,966 Let us execute. 136 00:06:18,967 --> 00:06:24,266 [No Audio] 137 00:06:24,267 --> 00:06:26,266 In summary, the Option enum can be used with 138 00:06:26,300 --> 00:06:28,800 almost any type of data and has two variants 139 00:06:28,801 --> 00:06:31,266 of Some or None. As a final example, we will 140 00:06:31,267 --> 00:06:33,766 see how to pass a variable of Option type as 141 00:06:33,767 --> 00:06:35,933 a function parameter. I will define the 142 00:06:35,934 --> 00:06:37,933 function called square, this function will 143 00:06:37,934 --> 00:06:40,600 compute the square of the value or the number 144 00:06:40,601 --> 00:06:42,700 that has been passed to it. The input and 145 00:06:42,701 --> 00:06:44,433 output of the function are both variable 146 00:06:44,434 --> 00:06:46,833 containing optional i32 values 147 00:06:46,834 --> 00:06:53,133 [No Audio] 148 00:06:53,134 --> 00:06:55,100 Next, inside the body of the function, I will 149 00:06:55,101 --> 00:06:56,700 use the match statement to check if the 150 00:06:56,701 --> 00:06:58,700 variable name is of type Some or None. 151 00:06:58,701 --> 00:07:05,400 [No Audio] 152 00:07:05,401 --> 00:07:07,833 First, I will include the arm for Some. 153 00:07:07,834 --> 00:07:12,066 [No Audio] 154 00:07:12,067 --> 00:07:13,900 In this case, I will return the square, 155 00:07:13,901 --> 00:07:15,833 encapsulated inside the Some. 156 00:07:15,834 --> 00:07:19,133 [No Audio] 157 00:07:19,134 --> 00:07:20,933 Since the return is an Option, 158 00:07:20,934 --> 00:07:23,933 therefore, we need to wrap the answer in some variant. 159 00:07:25,466 --> 00:07:28,266 Next, in case of non variant, I will return a None. 160 00:07:28,267 --> 00:07:31,933 [No Audio] 161 00:07:31,934 --> 00:07:34,033 Let us call this from the main function now. 162 00:07:35,633 --> 00:07:37,533 First I will define a variable having, 163 00:07:37,534 --> 00:07:39,166 having Some value. 164 00:07:39,167 --> 00:07:43,700 [No Audio] 165 00:07:43,701 --> 00:07:45,866 Next, I only want to print out the square of 166 00:07:45,867 --> 00:07:47,400 the value, when the call to the function 167 00:07:47,401 --> 00:07:50,466 returns some value. I will include an if 168 00:07:50,467 --> 00:07:52,633 statement for this, where I will check if the 169 00:07:52,634 --> 00:07:54,700 square, if the square of the number 170 00:07:54,701 --> 00:07:55,966 is not equal to None. 171 00:07:55,967 --> 00:08:03,000 [No Audio] 172 00:08:03,001 --> 00:08:05,145 In this case, I will print the square of the number. 173 00:08:05,146 --> 00:08:11,500 [No Audio] 174 00:08:11,501 --> 00:08:14,333 In case the function returns a None, so also we 175 00:08:14,334 --> 00:08:16,133 will display an appropriate message. 176 00:08:16,134 --> 00:08:21,600 [No Audio] 177 00:08:21,601 --> 00:08:22,833 Let us execute now. 178 00:08:22,834 --> 00:08:27,900 [No Audio] 179 00:08:27,901 --> 00:08:29,700 Let us also call this function with an 180 00:08:29,701 --> 00:08:31,333 a None variant and execute. 181 00:08:31,334 --> 00:08:37,600 [No Audio] 182 00:08:37,601 --> 00:08:39,933 Seems fine. Typically the Rust programmers 183 00:08:39,934 --> 00:08:42,265 use the match with enums, but it is just a 184 00:08:42,266 --> 00:08:44,265 convention and not a strict requirement, and 185 00:08:44,266 --> 00:08:46,066 you are free to consider the use of if 186 00:08:46,067 --> 00:08:48,566 statements also. That brings us to the end of 187 00:08:48,567 --> 00:08:50,700 this tutorial, we have learned an important 188 00:08:50,701 --> 00:08:52,600 type of enum called the Option enum, which 189 00:08:52,601 --> 00:08:54,533 will be very frequently used in Rust 190 00:08:54,534 --> 00:08:56,766 programming. The next tutorial is about the 191 00:08:56,767 --> 00:08:59,133 second very useful enum called Result, which 192 00:08:59,134 --> 00:09:02,366 is as important as the Option enum. Do come 193 00:09:02,367 --> 00:09:03,900 back for covering that, and until next 194 00:09:03,901 --> 00:09:06,233 tutorial, happy Rust programming. 195 00:09:06,234 --> 00:09:11,954 [No Audio]