1 00:00:00,000 --> 00:00:07,733 [No Audio] 2 00:00:07,734 --> 00:00:10,000 Welcome again. This tutorial is about the 3 00:00:10,001 --> 00:00:12,400 Match Statement. It is a control flow 4 00:00:12,401 --> 00:00:14,966 operator in Rust, that is used to transfer 5 00:00:14,967 --> 00:00:17,366 control to a particular block of code, based 6 00:00:17,367 --> 00:00:19,466 on the value of the variable being tested. 7 00:00:20,066 --> 00:00:22,033 Rust implements this construct using the 8 00:00:22,034 --> 00:00:25,033 match keyword. It has many similarities with 9 00:00:25,034 --> 00:00:26,700 the switch that is being used in other 10 00:00:26,701 --> 00:00:29,400 programming languages. Let's start learning 11 00:00:29,430 --> 00:00:30,833 the Match Statement. 12 00:00:31,551 --> 00:00:33,533 We will start with the general syntax first. 13 00:00:33,546 --> 00:00:47,333 [No Audio] 14 00:00:47,334 --> 00:00:49,766 Please note that the special syntax for the 15 00:00:49,767 --> 00:00:52,833 default case, where we just put an underscore 16 00:00:52,834 --> 00:00:54,666 and then equal to sign. 17 00:00:55,300 --> 00:00:57,166 This is a typical 18 00:00:57,167 --> 00:00:59,933 value of some variable that we want to match. 19 00:01:00,733 --> 00:01:02,933 Each of the possible values along with its 20 00:01:02,934 --> 00:01:05,099 respective statements to execute are called 21 00:01:05,100 --> 00:01:08,066 the arms of the match. In the above syntax, 22 00:01:08,067 --> 00:01:11,466 we have this as one arm, this is the second 23 00:01:11,467 --> 00:01:15,000 arm, and so on. When none of these arms 24 00:01:15,001 --> 00:01:17,566 matches, then the default case is going to be 25 00:01:17,567 --> 00:01:19,800 executed. One of the key property or 26 00:01:19,801 --> 00:01:22,233 characteristic of the match is that, it is 27 00:01:22,266 --> 00:01:25,400 exhaustive and covers all possible cases. And 28 00:01:25,401 --> 00:01:27,333 the second property is that, it always 29 00:01:27,334 --> 00:01:30,500 required that one of the arms should execute. 30 00:01:31,200 --> 00:01:33,000 These two properties will become clear to 31 00:01:33,001 --> 00:01:36,500 us in the examples. Also note that there are 32 00:01:36,501 --> 00:01:39,333 commas at the end of each arm. Let us start 33 00:01:39,334 --> 00:01:41,300 doing some examples. I will declare a 34 00:01:41,301 --> 00:01:43,866 variable some_number = 100. 35 00:01:43,867 --> 00:01:47,700 [No Audio] 36 00:01:47,701 --> 00:01:50,146 Next I will do a match on this variable. 37 00:01:50,147 --> 00:01:56,433 [No Audio] 38 00:01:56,434 --> 00:01:59,033 We will next add the arms for the match, the 39 00:01:59,034 --> 00:02:01,366 left side of the arms are the conditions or 40 00:02:01,367 --> 00:02:04,033 values, in this case, that we want to match, and 41 00:02:04,034 --> 00:02:06,633 the right side, our respective code that will 42 00:02:06,634 --> 00:02:08,500 be executed when the condition is being 43 00:02:08,501 --> 00:02:12,000 matched. I will match on the single value in 44 00:02:12,001 --> 00:02:14,866 this arm, and will add a suitable print statement. 45 00:02:14,867 --> 00:02:21,333 [No Audio] 46 00:02:21,334 --> 00:02:23,933 Next I will add another arm, where I will 47 00:02:23,934 --> 00:02:26,666 match on the two values. If the value happens 48 00:02:26,667 --> 00:02:29,600 to have a value of two or three, then I want 49 00:02:29,601 --> 00:02:31,233 this arm to be executed. 50 00:02:31,234 --> 00:02:37,200 [No Audio] 51 00:02:37,201 --> 00:02:40,000 A single pipe represents a logical OR in this case. 52 00:02:40,166 --> 00:02:42,300 Next, we can mention a range of values 53 00:02:42,333 --> 00:02:44,800 also. Range of values are being indicated by 54 00:02:44,801 --> 00:02:47,766 two dots, with a starting number and ending number. 55 00:02:48,233 --> 00:02:49,733 I would like to match on numbers 56 00:02:49,734 --> 00:02:51,466 from 4 to 100 inclusive. 57 00:02:51,473 --> 00:02:58,500 [No Audio] 58 00:02:58,501 --> 00:03:00,900 Finally, if none of the arm is being matched, 59 00:03:01,000 --> 00:03:03,800 then I would like to have a default arm. 60 00:03:04,466 --> 00:03:06,733 The default arm is included by having an 61 00:03:06,734 --> 00:03:08,800 underscore in the left side of the arm. 62 00:03:09,301 --> 00:03:11,033 I will also include a suitable print 63 00:03:11,034 --> 00:03:12,133 statement in this case. 64 00:03:12,139 --> 00:03:18,466 [No Audio] 65 00:03:18,467 --> 00:03:20,466 Now let us Cargo run this. 66 00:03:20,467 --> 00:03:25,633 [No Audio] 67 00:03:25,634 --> 00:03:27,862 You may note that, the value of the variable 68 00:03:27,863 --> 00:03:30,766 was 100, and it has matched the third arm, and 69 00:03:30,767 --> 00:03:32,566 therefore the respective print statement has 70 00:03:32,567 --> 00:03:35,766 been executed. Now let us explain a few points. 71 00:03:35,900 --> 00:03:38,233 If I check for the value of 2 in 72 00:03:38,234 --> 00:03:40,783 the first arm, by including the relevant condition, 73 00:03:40,784 --> 00:03:44,266 [No Audio] 74 00:03:44,267 --> 00:03:45,600 the compiler will show a message 75 00:03:45,601 --> 00:03:47,966 beneath the 2, in the second arm, telling me 76 00:03:47,967 --> 00:03:51,966 that it is an unreachable pattern. 77 00:03:51,967 --> 00:03:53,633 This is because in case of 2, the 78 00:03:53,634 --> 00:03:56,000 first time will execute first, and therefore 79 00:03:56,001 --> 00:03:58,400 the second arm will never get a chance. 80 00:03:58,600 --> 00:04:00,266 And though the program will execute, but the 81 00:04:00,267 --> 00:04:02,566 compiler has rightfully warned us of the 82 00:04:02,567 --> 00:04:05,633 possible issue. Let me remove it for now. 83 00:04:07,066 --> 00:04:09,533 Next, if we remove the default case and save, 84 00:04:09,800 --> 00:04:11,666 you may note that, the compiler will complain 85 00:04:11,667 --> 00:04:15,066 saying that, we have non-exhaustive patterns. 86 00:04:15,233 --> 00:04:17,933 The match requires all possible cases to be 87 00:04:17,934 --> 00:04:20,500 mentioned, so that at least one of the arm is 88 00:04:20,501 --> 00:04:23,433 always executed. If for some reason we 89 00:04:23,434 --> 00:04:25,700 mentioned all possible cases, then we may skip 90 00:04:25,701 --> 00:04:28,500 the default arm for the match. An example of 91 00:04:28,501 --> 00:04:31,800 all possible cases will be, when we do a 92 00:04:31,801 --> 00:04:34,000 match on a boolean variable, which takes only 93 00:04:34,001 --> 00:04:36,800 two values. So therefore, we can have one arm 94 00:04:36,801 --> 00:04:39,300 corresponding to true value, and another arm 95 00:04:39,301 --> 00:04:41,300 corresponding to the false value, and 96 00:04:41,301 --> 00:04:43,608 therefore the default arm will not be needed. 97 00:04:43,609 --> 00:04:46,400 [No Audio] 98 00:04:46,401 --> 00:04:50,166 Let us look at some more examples. Okay, let 99 00:04:50,167 --> 00:04:52,066 us look at the student grade assignment 100 00:04:52,067 --> 00:04:54,300 program, that we covered in the previous tutorial, 101 00:04:54,301 --> 00:04:56,233 but this time using the match statement. 102 00:04:56,733 --> 00:04:58,266 I will start by declaring a 103 00:04:58,267 --> 00:05:00,714 variable of marks, containing the marks of the student. 104 00:05:00,715 --> 00:05:05,433 [No Audio] 105 00:05:05,434 --> 00:05:07,300 I will now assign grades by doing a 106 00:05:07,301 --> 00:05:10,033 match on the marks. For storing the grades, 107 00:05:10,034 --> 00:05:11,433 I will declare another variable 108 00:05:11,437 --> 00:05:15,842 [No Audio] 109 00:05:15,843 --> 00:05:17,733 I will do a match on the marks 110 00:05:17,734 --> 00:05:24,033 [No Audio] 111 00:05:24,034 --> 00:05:26,433 Next, we will insert different arms 112 00:05:26,434 --> 00:05:28,300 corresponding to different grades. 113 00:05:28,301 --> 00:05:49,200 [No Audio] 114 00:05:49,201 --> 00:05:51,233 This program will assign the appropriate 115 00:05:51,234 --> 00:05:53,800 grade, based on the arm which is being matched. 116 00:05:54,033 --> 00:05:57,266 Please note that we match ranges of values, 117 00:05:57,267 --> 00:05:58,766 let us execute this. 118 00:05:58,767 --> 00:06:04,800 [No Audio] 119 00:06:04,801 --> 00:06:07,833 Please note again, that in contrast to, if-else-if, 120 00:06:07,834 --> 00:06:10,300 ladder, where the else part is optional, 121 00:06:10,301 --> 00:06:12,300 the requirement in the match is a bit different. 122 00:06:12,466 --> 00:06:14,566 And in majority of the cases, we may 123 00:06:14,567 --> 00:06:17,100 not skip the default option, and this is 124 00:06:17,101 --> 00:06:19,133 because of the match needs to be exhaustive, 125 00:06:19,134 --> 00:06:21,333 and needs to cover all possible cases. 126 00:06:21,833 --> 00:06:24,200 Additionally also note that, in case when we 127 00:06:24,201 --> 00:06:26,400 have one statement, so we may skip adding 128 00:06:26,401 --> 00:06:28,833 curly brackets to the statement after the arm. 129 00:06:29,833 --> 00:06:31,366 When we have multiple statements 130 00:06:31,367 --> 00:06:33,400 corresponding to a certain arm, then we will 131 00:06:33,401 --> 00:06:34,666 add curly brackets. 132 00:06:36,484 --> 00:06:38,233 Like the if let syntax, we 133 00:06:38,234 --> 00:06:41,166 can also have a let with the match 134 00:06:41,167 --> 00:06:43,966 statement, let us learn this. Let us first 135 00:06:43,967 --> 00:06:45,666 look at the general syntax. 136 00:06:45,667 --> 00:06:54,000 [No Audio] 137 00:06:54,001 --> 00:06:55,733 In this case now, the variable will be 138 00:06:55,734 --> 00:06:58,466 assigned a value, based on the matching arm result. 139 00:06:59,466 --> 00:07:01,633 Let us learn this with an example. 140 00:07:01,833 --> 00:07:03,666 Consider the same program that we just 141 00:07:03,667 --> 00:07:06,200 considered above, I will copy and paste it. 142 00:07:06,207 --> 00:07:12,600 [No Audio] 143 00:07:12,601 --> 00:07:14,233 Now, instead of assigning values to the 144 00:07:14,234 --> 00:07:16,700 variable of grade inside the individual arms, 145 00:07:16,701 --> 00:07:18,866 I will write let grade =, 146 00:07:19,100 --> 00:07:21,300 at start of the match, and will delete the 147 00:07:21,301 --> 00:07:24,200 assignment to the grade variables inside the arms. 148 00:07:24,201 --> 00:07:30,900 [No Audio] 149 00:07:30,901 --> 00:07:33,433 This is now a more compact and more refined 150 00:07:33,434 --> 00:07:35,615 version of the same code, let us execute. 151 00:07:35,616 --> 00:07:43,033 [No Audio] 152 00:07:43,034 --> 00:07:44,833 We may note, that the program executes the 153 00:07:44,834 --> 00:07:47,566 same way as in the previous case. The last 154 00:07:47,567 --> 00:07:49,633 point to note is that, sometimes you may have 155 00:07:49,634 --> 00:07:52,866 multiple statements inside a certain arm. In 156 00:07:52,867 --> 00:07:56,100 that case, if we are using the let syntax 157 00:07:56,300 --> 00:07:58,866 with the match, then the code associated with 158 00:07:58,867 --> 00:08:01,033 the arm should have at least one statement, 159 00:08:01,034 --> 00:08:03,433 which do not have a semicolon after it. 160 00:08:04,900 --> 00:08:07,966 For instance, for the first time, I will include 161 00:08:07,967 --> 00:08:09,733 some additional statement. 162 00:08:09,734 --> 00:08:16,533 [No Audio] 163 00:08:16,534 --> 00:08:19,300 If this arm gets executed, then the value of A 164 00:08:19,301 --> 00:08:21,733 will be assigned, because this is 165 00:08:21,734 --> 00:08:24,566 the statement which do not have a semicolon afterward. 166 00:08:24,733 --> 00:08:26,666 Another requirement with the let 167 00:08:26,667 --> 00:08:28,700 syntax is that, the returning value from the 168 00:08:28,701 --> 00:08:31,266 arms must have the same type. This 169 00:08:31,267 --> 00:08:34,900 requirement is the same in the if let syntax also. 170 00:08:35,466 --> 00:08:37,000 This is because the Rust, needs to 171 00:08:37,001 --> 00:08:39,232 know the type of the variables at compile 172 00:08:39,233 --> 00:08:41,866 time. And if it is unable to reduce the type, 173 00:08:41,900 --> 00:08:45,300 it will typically complain. For instance, if 174 00:08:45,301 --> 00:08:47,766 inside the first time, I return a numeric 175 00:08:47,767 --> 00:08:49,166 value instead of a char. 176 00:08:49,167 --> 00:08:53,059 [No Audio] 177 00:08:53,060 --> 00:08:53,933 Then you may note that, 178 00:08:53,934 --> 00:08:55,933 the compiler will show it or saying that, 179 00:08:56,100 --> 00:08:58,700 match arms have incompatible types. 180 00:08:59,333 --> 00:09:02,466 That is it for this particular tutorial. We looked at 181 00:09:02,467 --> 00:09:05,033 the details of the match, which share some 182 00:09:05,034 --> 00:09:07,200 similarities with the if statement. However, 183 00:09:07,433 --> 00:09:10,033 it has some essential differences at the same time. 184 00:09:10,433 --> 00:09:12,033 In the upcoming tutorial, we will be 185 00:09:12,034 --> 00:09:13,466 looking at the looping constructs. 186 00:09:14,100 --> 00:09:16,366 So do come back for learning those, 187 00:09:16,367 --> 00:09:18,573 and until next tutorial, happy Rust programming. 188 00:09:18,574 --> 00:09:23,600 [No Audio]