1 00:00:06,040 --> 00:00:12,910 Enumerations or e nums for short allow you to define a type by enumerating its possible variants. 2 00:00:12,910 --> 00:00:15,940 So let's start by looking at how to define an enum. 3 00:00:17,140 --> 00:00:19,510 So it's relatively the same as a shrug. 4 00:00:19,510 --> 00:00:24,820 So we'll say enum and then we'll create an enumeration of pets. 5 00:00:24,970 --> 00:00:31,000 So we'll call it Pet, and then we'll open and close it with curly brackets. 6 00:00:31,000 --> 00:00:34,780 And then in here we'll have a dog, a cat and a fish. 7 00:00:36,040 --> 00:00:43,270 So to use this, then we can just say let dog equals pet of dog. 8 00:00:45,010 --> 00:00:51,870 So just like structs, we can also implement methods on them. 9 00:00:51,880 --> 00:01:01,900 So let's pull pet open and close and then let's say let's create a function called What Am I? 10 00:01:02,290 --> 00:01:06,610 And remember, you have to reference yourself. 11 00:01:08,070 --> 00:01:10,380 And then we're going to return. 12 00:01:12,930 --> 00:01:13,950 String slice. 13 00:01:15,960 --> 00:01:19,260 And then inside here, we're just going to say, 14 00:01:22,080 --> 00:01:26,250 hey, match and we'll get into this in a second, self. 15 00:01:28,660 --> 00:01:29,410 Pet. 16 00:01:31,330 --> 00:01:31,810 Pet. 17 00:01:33,930 --> 00:01:34,470 Dog. 18 00:01:34,580 --> 00:01:41,490 Neither capitalize the p pet dog and if it matches, return. 19 00:01:42,660 --> 00:01:44,460 I am a dog. 20 00:01:45,150 --> 00:01:47,430 And then we're going to do this again for. 21 00:01:49,260 --> 00:01:50,580 Cat and fish. 22 00:01:59,230 --> 00:02:01,900 So there is something that I purposefully left out. 23 00:02:01,900 --> 00:02:03,750 So let's see what it is. 24 00:02:03,760 --> 00:02:09,070 It says we are expecting a string, but we return to string slice. 25 00:02:09,550 --> 00:02:13,420 So now let's return a reference to our string slice. 26 00:02:13,480 --> 00:02:22,120 But now when we do that, we need to specify our lifetime annotation and we want it to be a static lifetime. 27 00:02:23,140 --> 00:02:24,320 So now we're all good. 28 00:02:24,340 --> 00:02:29,680 So down here, let's, let's print this out just to make sure everything worked. 29 00:02:30,160 --> 00:02:33,820 So we're going to have a dog and we're going to say, What am I? 30 00:02:33,850 --> 00:02:36,570 So we're expecting it to say, I am a dog. 31 00:02:38,500 --> 00:02:40,070 And that's exactly what it says. 32 00:02:40,090 --> 00:02:41,110 It says, I am a doc. 33 00:02:41,110 --> 00:02:42,190 So awesome. 34 00:02:42,310 --> 00:02:43,120 It worked. 35 00:02:45,080 --> 00:02:49,310 So we can also do that for cat and fish and they would return. 36 00:02:49,310 --> 00:02:49,940 I am a cat. 37 00:02:49,940 --> 00:02:50,930 I am a fish. 38 00:02:50,930 --> 00:02:56,900 But now let's look at how we can use an enum inside of a struct. 39 00:02:57,050 --> 00:02:59,270 So we're going to shift gears for a second. 40 00:02:59,270 --> 00:03:04,400 We're going to create another enum called IP addr kind. 41 00:03:04,400 --> 00:03:14,300 So the IP address kind and we're going to have IP four and then also six. 42 00:03:14,750 --> 00:03:24,080 So now inside of here, we're going to create a struct for IP address and for our kind, we're going 43 00:03:24,080 --> 00:03:33,260 to assign it an IP address because our IP can either be IP V four or IPV six and then our address is 44 00:03:33,260 --> 00:03:35,990 obviously going to be a string. 45 00:03:38,390 --> 00:03:43,580 So down here, let's say let home equal I. 46 00:03:44,620 --> 00:03:45,270 Peter. 47 00:03:46,930 --> 00:03:48,880 And it's going the kind. 48 00:03:51,140 --> 00:03:55,490 Is going to be iPad or kind v four. 49 00:03:56,300 --> 00:04:00,380 And then our address is going to be a string. 50 00:04:03,730 --> 00:04:04,570 From. 51 00:04:07,840 --> 00:04:09,970 120 7001. 52 00:04:10,120 --> 00:04:11,860 So local host. 53 00:04:16,870 --> 00:04:21,520 And close it so we can also do let loopback 54 00:04:24,490 --> 00:04:26,320 IP adder. 55 00:04:28,490 --> 00:04:32,480 Kind, IP matter kind. 56 00:04:32,480 --> 00:04:35,870 And in this case, it's going to be version six. 57 00:04:35,870 --> 00:04:44,430 And then our address is going to be that. 58 00:04:46,680 --> 00:04:52,080 So that's how we can use an E numb inside of a stroke. 59 00:04:52,440 --> 00:04:56,220 We can also have structs inside of enemies. 60 00:04:56,220 --> 00:05:01,290 And then furthermore, we can assign data types inside our enum as well. 61 00:05:03,510 --> 00:05:09,690 So let's say we have this up here, so we're going to assign a data type to it, and we want this to 62 00:05:09,690 --> 00:05:10,620 be a string. 63 00:05:10,830 --> 00:05:16,410 So down here we can modify, we'll say let home. 64 00:05:18,150 --> 00:05:19,830 Equals I. 65 00:05:22,040 --> 00:05:23,000 Peter. 66 00:05:25,860 --> 00:05:31,050 V four and then we need it to be a string. 67 00:05:31,060 --> 00:05:37,890 So then we have string from 120 7001. 68 00:05:41,030 --> 00:05:42,020 So we have a little air. 69 00:05:42,050 --> 00:05:43,370 Let's see what it says. 70 00:05:43,520 --> 00:05:45,230 No associated name. 71 00:05:45,230 --> 00:05:48,020 And that's because this needs to say kind. 72 00:05:49,820 --> 00:05:57,410 So that's how we can assign a type inside of our enum so you can get creative as you want. 73 00:05:58,040 --> 00:06:02,660 With types inside of an enum, you can have variants with no data associated. 74 00:06:02,690 --> 00:06:05,120 Named fields like a stroke. 75 00:06:05,150 --> 00:06:11,010 You can specify a string or specify as many types inside, just like you can with a tuple. 76 00:06:11,030 --> 00:06:14,700 So the options are quite extensive, which is great. 77 00:06:14,720 --> 00:06:20,750 So now let's look at some enemies that rust gives us by default.