1 00:00:05,590 --> 00:00:08,770 Match is going to do exactly what it sounds like it will do. 2 00:00:08,800 --> 00:00:14,470 It is going to allow us to compare a value against a series of patterns and then execute code based 3 00:00:14,470 --> 00:00:15,850 on the matching pattern. 4 00:00:16,180 --> 00:00:21,840 So we did it earlier in one of our examples when we were setting up a pet a pet enum. 5 00:00:22,840 --> 00:00:28,750 And what we did was basically said, hey, match on yourself, and if you're a dog, I want you to print 6 00:00:28,750 --> 00:00:29,950 out I am a dog. 7 00:00:30,040 --> 00:00:33,970 So in essence, that's what the match statement does. 8 00:00:35,380 --> 00:00:44,590 So let's create another example and we'll create a new function called F and plus one. 9 00:00:45,820 --> 00:00:59,860 And we're going to take in an option I 32 and we want to return an option of Type II 32. 10 00:01:00,460 --> 00:01:03,820 And then in here we're going to say we want to match on X. 11 00:01:06,130 --> 00:01:10,390 If none is passed in, then that's what we want to return. 12 00:01:10,390 --> 00:01:10,930 None. 13 00:01:12,180 --> 00:01:13,950 But if something. 14 00:01:15,600 --> 00:01:16,800 This past in. 15 00:01:21,570 --> 00:01:24,690 Then we want to return some. 16 00:01:26,330 --> 00:01:28,970 Of I plus one. 17 00:01:30,680 --> 00:01:44,170 So when we run this or call this, I should say we'll say let five equals sum I. 18 00:01:44,780 --> 00:01:45,230 Right. 19 00:01:45,230 --> 00:01:46,400 So this is what. 20 00:01:47,210 --> 00:01:51,410 This is essentially what's going to get converted to some five right there. 21 00:01:51,410 --> 00:01:52,400 That's some of I. 22 00:01:53,150 --> 00:01:59,120 And then we say let six equals plus one of five. 23 00:02:03,960 --> 00:02:07,950 And then we'll say Let none equals. 24 00:02:08,950 --> 00:02:10,360 Plus one. 25 00:02:11,770 --> 00:02:12,370 Of none. 26 00:02:13,990 --> 00:02:14,920 And then let's print it out. 27 00:02:14,920 --> 00:02:16,630 So we'll print it out. 28 00:02:22,220 --> 00:02:26,090 And we'll have six in here. 29 00:02:26,090 --> 00:02:27,920 And it's probably going to want it to be. 30 00:02:30,770 --> 00:02:31,610 Just like that. 31 00:02:32,840 --> 00:02:33,890 Back to the terminal. 32 00:02:34,640 --> 00:02:35,870 Cargo run it. 33 00:02:35,900 --> 00:02:39,240 And that's exactly what we got returned with some six. 34 00:02:39,260 --> 00:02:46,700 So now if we come back in here and we do none, we would expect none to be returned. 35 00:02:47,000 --> 00:02:48,460 And that's exactly what happened. 36 00:02:48,470 --> 00:02:51,050 So what this function did. 37 00:02:52,130 --> 00:03:00,680 Was it accepted a parameter of X which we passed in here was five, and then inside of the five variable 38 00:03:00,680 --> 00:03:06,110 contained some five and we matched on some of I. 39 00:03:06,140 --> 00:03:08,840 So any value could have been passed in here. 40 00:03:08,840 --> 00:03:14,300 And then whatever the value that sum contained, we added one to it. 41 00:03:16,280 --> 00:03:21,500 So what happens if we forget to add in the num statement? 42 00:03:23,570 --> 00:03:25,100 We already see we get an air coming. 43 00:03:25,100 --> 00:03:29,970 So it says we've got an air pattern. 44 00:03:29,990 --> 00:03:33,890 None not covered, so the compiler will catch it for us. 45 00:03:33,920 --> 00:03:41,390 Matches in rust are exhaustive, meaning that every possibility must be exhausted for the code to be 46 00:03:41,390 --> 00:03:41,990 valid. 47 00:03:41,990 --> 00:03:47,600 So we need to make sure that we cover every case because otherwise it will likely introduce a bug in 48 00:03:47,600 --> 00:03:48,200 our code. 49 00:03:49,020 --> 00:03:55,050 So we can use catch all patterns to be able to handle all other situations. 50 00:03:55,080 --> 00:04:01,230 So let me come back in here and we'll just re put in none. 51 00:04:05,840 --> 00:04:13,700 And then let's go back and look at our pet name and show that for all other pets, we will print out 52 00:04:13,700 --> 00:04:15,050 something different. 53 00:04:17,640 --> 00:04:20,310 So we want to come up here and. 54 00:04:22,080 --> 00:04:27,930 Actually, we'll come down here and we'll create a function that says What pet? 55 00:04:28,320 --> 00:04:34,020 And then in here we'll take our input, which is going to be a reference to a string slice. 56 00:04:34,020 --> 00:04:39,270 And then we don't need to return anything in this case, but we're going to match on input. 57 00:04:43,270 --> 00:04:49,540 So what we'll do in here is we'll say, Hey, we have dog and we're going to return. 58 00:04:54,380 --> 00:04:56,210 I have a dog. 59 00:04:57,260 --> 00:05:01,190 And then for all of our other cases, we'll have. 60 00:05:04,070 --> 00:05:04,760 Are. 61 00:05:06,390 --> 00:05:07,050 Fish. 62 00:05:10,070 --> 00:05:11,750 We'll have our cat. 63 00:05:13,650 --> 00:05:20,880 And then we can see that our input is already kind of like kitten to us, Hey, something needs to be 64 00:05:21,450 --> 00:05:21,870 covered. 65 00:05:21,870 --> 00:05:27,780 So if we hover over it, it'll tell us the non-exhaustive pattern is not covered and it's telling us 66 00:05:27,780 --> 00:05:29,190 to use an underscore. 67 00:05:29,190 --> 00:05:34,020 So if we put in an underscore here and then just say, Hey. 68 00:05:35,830 --> 00:05:39,640 I have no clue what pet you have. 69 00:05:45,350 --> 00:05:54,350 Well, now it seems to work because we're using this underscore to cover all other situations. 70 00:05:55,730 --> 00:05:58,640 So now let's test this out real quick. 71 00:05:58,640 --> 00:06:13,580 We'll say what pet and we'll say dog, what pet cat, what pet and cow. 72 00:06:14,570 --> 00:06:22,040 So now if we run this, we expect to see I have a dog, I have a cat and I have no clue what pet you 73 00:06:22,040 --> 00:06:22,610 have. 74 00:06:22,820 --> 00:06:24,470 And that's exactly what we got. 75 00:06:24,620 --> 00:06:30,530 So it's important to remember that match is exhaustive. 76 00:06:30,530 --> 00:06:35,510 So we have to try to cover every single case that we could have. 77 00:06:35,810 --> 00:06:38,600 And then we can use this little underscore right here. 78 00:06:39,640 --> 00:06:46,510 To basically say, hey, if it doesn't match anything else, then by default run this.