1 00:00:06,600 --> 00:00:08,610 - An enum is a data type 2 00:00:08,610 --> 00:00:11,370 that has a closed set of allowed values. 3 00:00:11,370 --> 00:00:12,870 So for example here, 4 00:00:12,870 --> 00:00:15,810 a flight enum type might allow you to book a ticket 5 00:00:15,810 --> 00:00:18,570 in economy class, business class, or first class, 6 00:00:18,570 --> 00:00:19,860 but nothing else. 7 00:00:19,860 --> 00:00:22,140 It's a closed set of allowed values 8 00:00:22,140 --> 00:00:23,790 like a finite state machine 9 00:00:23,790 --> 00:00:26,460 and we could use integers to achieve the same effect 10 00:00:26,460 --> 00:00:28,170 like an integer that could be zero 11 00:00:28,170 --> 00:00:29,850 to represent the economy class, 12 00:00:29,850 --> 00:00:31,950 one to represent business class, 13 00:00:31,950 --> 00:00:33,630 two to represent first class, 14 00:00:33,630 --> 00:00:36,690 but enums are more self-describing. 15 00:00:36,690 --> 00:00:39,720 They have a name and they only allow you to assign 16 00:00:39,720 --> 00:00:41,910 one of the close set of values. 17 00:00:41,910 --> 00:00:45,840 So it turns out the enums are really important in Rust. 18 00:00:45,840 --> 00:00:49,110 There are enums that are defined in the language itself 19 00:00:49,110 --> 00:00:52,080 and there are enums that we define in our own code. 20 00:00:52,080 --> 00:00:54,150 So we'll have a look at both techniques. 21 00:00:54,150 --> 00:00:55,950 First of all, we'll see how to define 22 00:00:55,950 --> 00:00:58,290 our own enum types, quite simple. 23 00:00:58,290 --> 00:00:59,520 Then later on in this lesson, 24 00:00:59,520 --> 00:01:01,980 we'll see how to use some of the existing types, 25 00:01:01,980 --> 00:01:04,980 enum types are defined in the language. 26 00:01:04,980 --> 00:01:07,860 So to define an enum type is quite straightforward. 27 00:01:07,860 --> 00:01:09,780 There's a keyword called enum. 28 00:01:09,780 --> 00:01:11,250 You give your enum type a name 29 00:01:11,250 --> 00:01:12,600 starting with a capital letter. 30 00:01:12,600 --> 00:01:14,850 Rust is quite fussy about that. 31 00:01:14,850 --> 00:01:16,920 And then you have curly brackets. 32 00:01:16,920 --> 00:01:18,270 And then inside the curly brackets, 33 00:01:18,270 --> 00:01:20,910 you basically list all the allowed possible values 34 00:01:20,910 --> 00:01:21,960 for that enum type. 35 00:01:21,960 --> 00:01:25,650 And those allowed values are called variants in Rust. 36 00:01:25,650 --> 00:01:30,120 Other languages call them mnemonics or fields or states, 37 00:01:30,120 --> 00:01:33,510 but the word that Rust uses is variant. 38 00:01:33,510 --> 00:01:34,800 So here's an example. 39 00:01:34,800 --> 00:01:36,810 I've got a simple enum type called Color. 40 00:01:36,810 --> 00:01:38,220 It has three variants, 41 00:01:38,220 --> 00:01:40,407 three allowed values: red, green, and blue. 42 00:01:40,407 --> 00:01:43,350 You can only assign the value red, green, or blue 43 00:01:43,350 --> 00:01:45,060 to a Color variable. 44 00:01:45,060 --> 00:01:47,820 So you would declare the enum type in one part of your code 45 00:01:47,820 --> 00:01:49,740 and then you could use it elsewhere. 46 00:01:49,740 --> 00:01:52,680 So to use an enum type, again, it's quite straightforward. 47 00:01:52,680 --> 00:01:55,260 You say the enum type, like color, 48 00:01:55,260 --> 00:01:57,180 a couple of colons, double colon, 49 00:01:57,180 --> 00:01:58,740 it's like a scope operator, 50 00:01:58,740 --> 00:02:00,960 followed by the variant you want. 51 00:02:00,960 --> 00:02:03,180 And then you typically use a match statement 52 00:02:03,180 --> 00:02:04,470 to test the value. 53 00:02:04,470 --> 00:02:05,820 Okay, so you could use an if statement, 54 00:02:05,820 --> 00:02:07,470 but a match statement actually works quite well. 55 00:02:07,470 --> 00:02:08,433 It looks like this. 56 00:02:09,600 --> 00:02:13,050 In this example, I've declared an enum variable c, 57 00:02:13,050 --> 00:02:15,540 c is of type Color, 58 00:02:15,540 --> 00:02:17,760 so I can only assign it one of the allowed values, 59 00:02:17,760 --> 00:02:19,320 red, green, or blue. 60 00:02:19,320 --> 00:02:22,410 If I decided or tried to assign anything else, 61 00:02:22,410 --> 00:02:24,240 I get an error. 62 00:02:24,240 --> 00:02:26,940 And you do need to specify the enum type here. 63 00:02:26,940 --> 00:02:28,500 You couldn't just say red. 64 00:02:28,500 --> 00:02:31,020 You gotta say enum type colon colon red. 65 00:02:31,020 --> 00:02:32,940 It is properly scoped. 66 00:02:32,940 --> 00:02:35,370 And then you test the value for c. 67 00:02:35,370 --> 00:02:38,220 If it is a value red, then output coch, 68 00:02:38,220 --> 00:02:40,710 coch is the Welsh word for red. 69 00:02:40,710 --> 00:02:43,080 If it's green, it outputs gwyrdd. 70 00:02:43,080 --> 00:02:44,850 That's the Welsh word for green. 71 00:02:44,850 --> 00:02:46,770 Or if it's blue, you output glas. 72 00:02:46,770 --> 00:02:48,450 That's the Welsh word for blue. 73 00:02:48,450 --> 00:02:51,150 Okay, so quite straightforward really. 74 00:02:51,150 --> 00:02:52,860 What you probably do in practice 75 00:02:52,860 --> 00:02:55,830 is to put enums into a separate file. 76 00:02:55,830 --> 00:02:56,663 We haven't really talked 77 00:02:56,663 --> 00:02:59,370 about multi-file projects yet in Rust, 78 00:02:59,370 --> 00:03:01,410 so we're gonna start doing that now. 79 00:03:01,410 --> 00:03:04,260 A Rust project can have lots of files. 80 00:03:04,260 --> 00:03:06,990 The main RS file is obviously the entry point, 81 00:03:06,990 --> 00:03:10,680 but you can have other files as well kind of collaborating 82 00:03:10,680 --> 00:03:14,070 and those other files are called modules or module files. 83 00:03:14,070 --> 00:03:18,450 So here I define a module file called mytypes.rs. 84 00:03:18,450 --> 00:03:20,850 You can call it anything you want. 85 00:03:20,850 --> 00:03:25,053 I've declared my Color enum type in this module file. 86 00:03:26,130 --> 00:03:30,330 By default, when you define classes and enums 87 00:03:30,330 --> 00:03:33,360 and functions and such in a separate module file, 88 00:03:33,360 --> 00:03:35,820 by default, it's private to that file 89 00:03:35,820 --> 00:03:38,370 which means you can only use it in that file. 90 00:03:38,370 --> 00:03:40,830 To be able to use it in a different file, 91 00:03:40,830 --> 00:03:42,120 you have to use the pub keyword. 92 00:03:42,120 --> 00:03:45,510 Pub is the keyword that means public in Rust. 93 00:03:45,510 --> 00:03:50,510 Okay, so I'm declaring my enum Color as being a public enum. 94 00:03:51,090 --> 00:03:53,880 I can use it in other files. 95 00:03:53,880 --> 00:03:58,020 The variants are implicitly public anyway 96 00:03:58,020 --> 00:04:01,110 so as long as the enum type itself is public, 97 00:04:01,110 --> 00:04:03,030 then the variants are also public, 98 00:04:03,030 --> 00:04:05,940 meaning you can access the red, green, and blue values 99 00:04:05,940 --> 00:04:07,650 in a different file. 100 00:04:07,650 --> 00:04:12,650 So you would declare your enum type in a file, mytypes.rs, 101 00:04:13,080 --> 00:04:17,190 and then you've gotta import this file into your main code. 102 00:04:17,190 --> 00:04:19,290 Okay, so in your main code, 103 00:04:19,290 --> 00:04:22,410 you load the module into main.rs. 104 00:04:22,410 --> 00:04:24,210 You use the mod keyword. 105 00:04:24,210 --> 00:04:26,220 When you use the mod keyword like this 106 00:04:26,220 --> 00:04:28,950 and you just give it basically the name of the file 107 00:04:28,950 --> 00:04:31,380 but without the .rs extension, 108 00:04:31,380 --> 00:04:34,650 it's a bit like the way you import modules in Python. 109 00:04:34,650 --> 00:04:37,380 Similar-ish to that. 110 00:04:37,380 --> 00:04:40,590 It will look for a file called mytypes.rs 111 00:04:40,590 --> 00:04:44,280 and it will import this code into your application. 112 00:04:44,280 --> 00:04:46,380 You have to import the module 113 00:04:46,380 --> 00:04:49,590 in order for this file to be noticed. 114 00:04:49,590 --> 00:04:51,600 If you just declared this file 115 00:04:51,600 --> 00:04:54,570 and then you forgot to declare it as a module, 116 00:04:54,570 --> 00:04:55,800 it wouldn't load it. 117 00:04:55,800 --> 00:04:57,570 It would just ignore this file altogether. 118 00:04:57,570 --> 00:05:01,320 You have to explicitly load the My Types module 119 00:05:01,320 --> 00:05:03,273 into your main application. 120 00:05:04,380 --> 00:05:06,210 Once you've done that, 121 00:05:06,210 --> 00:05:09,510 you can then use an alias if you like 122 00:05:09,510 --> 00:05:13,710 or introduce the My Types color name into scope. 123 00:05:13,710 --> 00:05:16,710 The use keyword brings a symbol name 124 00:05:16,710 --> 00:05:20,100 directly into scope in your current file. 125 00:05:20,100 --> 00:05:22,590 Okay, so first of all, import the module. 126 00:05:22,590 --> 00:05:24,780 So the module is kind of processed, 127 00:05:24,780 --> 00:05:27,150 that code now takes effect. 128 00:05:27,150 --> 00:05:31,680 And then inside My Types, there's a color enum. 129 00:05:31,680 --> 00:05:34,410 Bring the color enum directly into scope. 130 00:05:34,410 --> 00:05:39,060 I can now use the color enum type directly in my code here. 131 00:05:39,060 --> 00:05:40,980 I'm gonna run an example in a minute 132 00:05:40,980 --> 00:05:42,990 and I'm gonna show various different combinations 133 00:05:42,990 --> 00:05:45,810 to show what would happen if you didn't have the statement 134 00:05:45,810 --> 00:05:47,820 or if you didn't have that statement 135 00:05:47,820 --> 00:05:50,720 so you can kind of understand what the effect actually is.