1 00:00:05,940 --> 00:00:14,310 So in this lecture, I want to look at how and where we can use just additional patterns and the type 2 00:00:14,310 --> 00:00:17,070 of syntax is we have available to us. 3 00:00:17,340 --> 00:00:21,840 So there are many different syntax and ways that we can. 4 00:00:23,440 --> 00:00:26,080 Create a match arm. 5 00:00:26,440 --> 00:00:32,200 But I just want to introduce you to a few, just to kind of show you what all is available to us. 6 00:00:32,470 --> 00:00:36,490 So let's create a variable X and assign it to one. 7 00:00:37,450 --> 00:00:40,120 And then we're going to match on X. 8 00:00:41,020 --> 00:00:45,220 And then inside here we want to say one or two. 9 00:00:45,490 --> 00:00:51,430 So if the value of X is one or two, then we want to print out one or two. 10 00:00:52,210 --> 00:00:57,400 And then to make our match exhaustive, we want to say anything else. 11 00:00:57,670 --> 00:00:58,990 Let's print out. 12 00:01:00,070 --> 00:01:01,840 Not one or two. 13 00:01:03,160 --> 00:01:10,660 So when we run this with X being one, we expect it to print out one or two, which is exactly what 14 00:01:10,660 --> 00:01:11,390 happens. 15 00:01:11,410 --> 00:01:18,850 And then just to verify that we are getting the desired behavior, change X to ten and rerun it, and 16 00:01:18,850 --> 00:01:20,980 we see that we get not one or two. 17 00:01:21,220 --> 00:01:29,890 So this syntax up here with this operator is the OR call the OR operator, I should say. 18 00:01:29,950 --> 00:01:35,230 And what it's doing is it is saying, hey, if X is one or two. 19 00:01:35,840 --> 00:01:36,410 Then. 20 00:01:37,260 --> 00:01:38,460 This statement is true. 21 00:01:38,490 --> 00:01:42,330 So execute the following code, which is exactly what happened. 22 00:01:43,920 --> 00:01:47,970 So let's comment that out and let's look at a different way. 23 00:01:48,900 --> 00:01:52,350 So now we're going to match on. 24 00:01:53,110 --> 00:02:00,220 An exclusive range of values so we'll leave x is equal to ten will match on X again. 25 00:02:01,680 --> 00:02:04,620 And in this case, we'll say 1 to 5. 26 00:02:05,220 --> 00:02:10,950 And if it matches in that range, we'll print out matches. 27 00:02:13,090 --> 00:02:15,850 Otherwise we're going to print out. 28 00:02:20,100 --> 00:02:21,060 Not matching. 29 00:02:22,830 --> 00:02:25,260 So we see that we've got our little red squiggly. 30 00:02:25,290 --> 00:02:26,740 Let's check out what it says. 31 00:02:26,760 --> 00:02:31,200 And it's telling us the exclusive range pattern is experimental. 32 00:02:31,230 --> 00:02:35,280 So to fix this, we need to add in the little equal sign. 33 00:02:35,280 --> 00:02:40,000 So now it's going to be 1 to 5, which includes five. 34 00:02:40,020 --> 00:02:44,910 So with our X being ten, we expect this to print out, not matching. 35 00:02:46,250 --> 00:02:52,640 But when we go to five, I anticipate it saying that it matches, which is exactly what happened. 36 00:02:52,640 --> 00:03:01,820 So this is how we can use an inclusive range to check over a a range of values to see if something matches. 37 00:03:03,050 --> 00:03:13,280 And then lastly, we can use an if an excuse me and if condition specified after the pattern and a match. 38 00:03:13,280 --> 00:03:14,390 So let's check this out. 39 00:03:14,390 --> 00:03:24,170 So let's say let x equals to sum five and then we'll say let y equals five, and then we're going to 40 00:03:24,170 --> 00:03:25,370 match on x. 41 00:03:27,060 --> 00:03:30,810 And we'll say some ten. 42 00:03:33,250 --> 00:03:42,840 And then we're going to print out ten, but then we're going to do some x. 43 00:03:44,430 --> 00:03:50,460 If X equals Y, then we want to print out. 44 00:03:52,940 --> 00:03:53,720 Matches. 45 00:03:55,700 --> 00:04:03,170 And then otherwise, we just want to print out our default statement and for ease, we'll just print 46 00:04:03,170 --> 00:04:04,780 out default. 47 00:04:04,790 --> 00:04:09,650 So now we have some five Y equals to five and we're going to match on X. 48 00:04:09,650 --> 00:04:13,250 So when we run this, we see that it says it matches. 49 00:04:14,760 --> 00:04:17,130 So how is this working? 50 00:04:17,310 --> 00:04:26,160 So we're passing in X to our match statement and then X is some value and then X is in here as well, 51 00:04:26,460 --> 00:04:29,120 which in this case it is going to be five. 52 00:04:29,130 --> 00:04:36,360 So if five, which is referring to this x right here, equals y, which is five. 53 00:04:37,360 --> 00:04:39,510 Then in this case, we know that five equals five. 54 00:04:39,520 --> 00:04:40,090 So. 55 00:04:41,270 --> 00:04:43,970 We are able to execute this block of code. 56 00:04:44,270 --> 00:04:47,000 So now if I change this to ten. 57 00:04:47,420 --> 00:04:53,000 So X is going to be some ten, then we anticipate ten being printed out. 58 00:04:54,240 --> 00:04:56,370 Which is exactly what happens. 59 00:04:56,670 --> 00:05:01,830 And then obviously, if I put in 15 here, so we're not going to. 60 00:05:02,770 --> 00:05:04,180 This is going to be false. 61 00:05:04,450 --> 00:05:05,600 This will be false. 62 00:05:05,630 --> 00:05:09,940 Well, then we expect default to be printed and that's what we get. 63 00:05:11,120 --> 00:05:17,540 So through the last few lectures we have learned about idioms and pattern matching, which is a very 64 00:05:17,540 --> 00:05:19,490 powerful feature Rust has. 65 00:05:19,490 --> 00:05:23,900 And as I mentioned earlier, we were going to cover generics in the next section. 66 00:05:23,900 --> 00:05:27,980 So let's continue on with what traits and generics are.