1 00:00:06,600 --> 00:00:08,880 - So we're gonna have a look at a demo. 2 00:00:08,880 --> 00:00:12,600 For this lesson, all the demos are in this project, 3 00:00:12,600 --> 00:00:14,490 lesson04_enums. 4 00:00:14,490 --> 00:00:18,990 I created this project using Cargo, let's take a look. 5 00:00:18,990 --> 00:00:23,370 So here it is on my file system in rustdev, lesson04. 6 00:00:23,370 --> 00:00:25,530 It's a Cargo project, 7 00:00:25,530 --> 00:00:27,930 so if you open that project in the code editor, 8 00:00:27,930 --> 00:00:30,630 I'm gonna open that project in VS Code. 9 00:00:30,630 --> 00:00:34,980 So here it is, here's my project in VS Code. 10 00:00:34,980 --> 00:00:37,113 And if you look in the source folder, 11 00:00:38,580 --> 00:00:41,250 ignore this file for now, 12 00:00:41,250 --> 00:00:44,100 we'll discuss that later on. 13 00:00:44,100 --> 00:00:47,910 Just for now, notice mytypes.rs, 14 00:00:47,910 --> 00:00:50,430 I define an enum type called color 15 00:00:50,430 --> 00:00:52,650 with the variance red, green, and blue; 16 00:00:52,650 --> 00:00:54,900 and that enum is public. 17 00:00:54,900 --> 00:00:58,170 I define another enum type called house location, 18 00:00:58,170 --> 00:01:00,030 let's ignore that for now, 19 00:01:00,030 --> 00:01:03,810 we'll discuss that later on in this lesson 20 00:01:03,810 --> 00:01:06,393 so imagine that that didn't exist for now. 21 00:01:07,323 --> 00:01:10,170 And we'll just concentrate on the color enum type. 22 00:01:10,170 --> 00:01:13,590 So mytypes.rs is a module file, 23 00:01:13,590 --> 00:01:16,290 in order for that module to become part of my application, 24 00:01:16,290 --> 00:01:19,470 even though it's part of the directory structure 25 00:01:19,470 --> 00:01:23,460 it isn't actually part of my application unless I load it. 26 00:01:23,460 --> 00:01:25,740 And you load it from the top level, 27 00:01:25,740 --> 00:01:30,210 in your top level file main.rs, it looks like this. 28 00:01:30,210 --> 00:01:33,390 You load mytypes, like so. 29 00:01:33,390 --> 00:01:37,380 It effectively will look for mytypes.rs, 30 00:01:37,380 --> 00:01:41,100 and that code in there gets imported at this point here, 31 00:01:41,100 --> 00:01:45,300 it's like an import statement in other languages, okay? 32 00:01:45,300 --> 00:01:48,990 And then I've introduced the color enum type 33 00:01:48,990 --> 00:01:51,960 and the house location enum type into scope. 34 00:01:51,960 --> 00:01:56,960 It means I can use these enum types directly in my code now. 35 00:01:57,000 --> 00:02:00,150 I could have split this into two separate statements, 36 00:02:00,150 --> 00:02:01,230 I could have written it like this 37 00:02:01,230 --> 00:02:03,360 and I just do a bit of copying and pasting. 38 00:02:03,360 --> 00:02:08,250 I could've imported the color name if you like, symbol name, 39 00:02:08,250 --> 00:02:10,470 from mytypes into scope, 40 00:02:10,470 --> 00:02:12,420 and then separately I could have imported 41 00:02:12,420 --> 00:02:16,260 the house location also into scope. 42 00:02:16,260 --> 00:02:19,890 Okay, so you can input that type into scope, 43 00:02:19,890 --> 00:02:22,590 into the current scope from that module, 44 00:02:22,590 --> 00:02:26,070 and you can input that type into scope from that module. 45 00:02:26,070 --> 00:02:28,350 Because they're located in the same module, 46 00:02:28,350 --> 00:02:30,720 then it makes sense to group them together, 47 00:02:30,720 --> 00:02:32,550 so you would do that. 48 00:02:32,550 --> 00:02:36,330 From the mytypes module introduce that name 49 00:02:36,330 --> 00:02:37,770 and that name into scope, 50 00:02:37,770 --> 00:02:41,223 it means I can use these names directly now in my code. 51 00:02:42,240 --> 00:02:46,500 There's some other examples we're gonna look at later on, 52 00:02:46,500 --> 00:02:48,480 we're gonna be talk looking at mytypes 53 00:02:48,480 --> 00:02:50,520 for demo purposes later on, 54 00:02:50,520 --> 00:02:54,180 which is kind of similar but slightly different. 55 00:02:54,180 --> 00:02:56,100 So I'm gonna ignore that for now, 56 00:02:56,100 --> 00:02:57,540 and we'll ignore this for now, 57 00:02:57,540 --> 00:02:59,760 we'll come back and look at that later on, 58 00:02:59,760 --> 00:03:01,023 okay, when we're ready. 59 00:03:02,730 --> 00:03:05,250 Okay, let me just show you what functions I've got. 60 00:03:05,250 --> 00:03:08,100 These are all the functions for this lesson, 61 00:03:08,100 --> 00:03:09,900 so like we had in the previous lesson 62 00:03:09,900 --> 00:03:11,250 I've got a main function 63 00:03:11,250 --> 00:03:14,310 which is kind of like the driver for the demo, 64 00:03:14,310 --> 00:03:17,790 and then it calls one of several other functions 65 00:03:17,790 --> 00:03:21,360 depending on which part of the demo I want to run. 66 00:03:21,360 --> 00:03:24,900 So there's a function here which demonstrates simple enums, 67 00:03:24,900 --> 00:03:26,300 I'm going to uncomment that, 68 00:03:27,900 --> 00:03:29,910 and let's have a look at the code in here. 69 00:03:29,910 --> 00:03:31,380 Basically the code in here 70 00:03:31,380 --> 00:03:34,350 is what we showed in the PowerPoints. 71 00:03:34,350 --> 00:03:36,863 So it demonstrates simple enums, 72 00:03:37,801 --> 00:03:42,360 it declares a variable of type color and it's color red, 73 00:03:42,360 --> 00:03:43,890 and then we match on the color, 74 00:03:43,890 --> 00:03:46,203 we output either coch, gwyrdd, or glass. 75 00:03:47,220 --> 00:03:49,860 Right, so I'm gonna run this application several times 76 00:03:49,860 --> 00:03:52,470 because I've got several different things I wanna show, 77 00:03:52,470 --> 00:03:54,370 so let me open up the terminal window. 78 00:03:56,070 --> 00:03:58,140 Now I'm gonna warn you in advance, 79 00:03:58,140 --> 00:03:59,370 when I run the application 80 00:03:59,370 --> 00:04:03,690 I'm gonna get quite a lot of warnings about dead code, okay? 81 00:04:03,690 --> 00:04:05,940 So I'll run it 82 00:04:05,940 --> 00:04:08,140 and then I'll explain what the warnings are, 83 00:04:09,300 --> 00:04:12,390 and then we'll see how to kind of look 84 00:04:12,390 --> 00:04:14,070 at some other techniques as well. 85 00:04:14,070 --> 00:04:16,713 So let's just do a cargo run, first of all. 86 00:04:19,260 --> 00:04:20,880 So I'm gonna make this a bit bigger, 87 00:04:20,880 --> 00:04:22,710 I'm just gonna show you what the warnings were. 88 00:04:22,710 --> 00:04:25,500 There were six different warnings, I think. 89 00:04:25,500 --> 00:04:28,500 Some of the warnings we kind of know about already, 90 00:04:28,500 --> 00:04:32,520 I've got a function demo enum with data, 91 00:04:32,520 --> 00:04:34,530 it's complaining that I've got that function 92 00:04:34,530 --> 00:04:36,090 but I've never used it. 93 00:04:36,090 --> 00:04:41,090 I've got another function called demo using option enum, 94 00:04:41,280 --> 00:04:43,800 option is one of the built-in enum types, 95 00:04:43,800 --> 00:04:45,997 it's complaining that I've not used that function. 96 00:04:45,997 --> 00:04:46,870 "Why have you written it?" 97 00:04:46,870 --> 00:04:49,270 Well, you know, because it's just a simple demo. 98 00:04:50,160 --> 00:04:55,160 Oh, I've got another function here, second of day. 99 00:04:55,320 --> 00:04:57,450 That's a curious one, second of day. 100 00:04:57,450 --> 00:04:59,280 We'll have a look at that later. 101 00:04:59,280 --> 00:05:01,200 And other functions as well, 102 00:05:01,200 --> 00:05:02,490 which is also complaining about 103 00:05:02,490 --> 00:05:04,800 this demo using result enum, 104 00:05:04,800 --> 00:05:09,030 result is another enum type in Rust. 105 00:05:09,030 --> 00:05:11,250 So it's complaining about the functions that exist 106 00:05:11,250 --> 00:05:13,260 but haven't yet been called, 107 00:05:13,260 --> 00:05:16,710 but also it's complaining 108 00:05:16,710 --> 00:05:19,350 I've got an enum type called house location. 109 00:05:19,350 --> 00:05:22,440 If I remind you, right at the top, 110 00:05:22,440 --> 00:05:25,170 I imported the house location. 111 00:05:25,170 --> 00:05:28,070 Well, actually what I did, I imported the module, mytypes, 112 00:05:29,250 --> 00:05:33,840 and in there it has the color enum, which I'm using, 113 00:05:33,840 --> 00:05:36,450 it has the house location enum which I'm not using yet, 114 00:05:36,450 --> 00:05:39,150 I will do later, but I'm not using it just yet. 115 00:05:39,150 --> 00:05:43,290 In the code it's saying, "Why have you defined this enum 116 00:05:43,290 --> 00:05:45,000 if you're not using it?" 117 00:05:45,000 --> 00:05:47,640 Well, because I haven't got round to it yet is the answer, 118 00:05:47,640 --> 00:05:49,390 but it's still giving me a warning. 119 00:05:51,090 --> 00:05:54,363 So, well, yeah, so there we are. 120 00:05:55,890 --> 00:05:59,130 And then it actually output what I was hoping to output, 121 00:05:59,130 --> 00:06:04,130 so back in main what I called my simple function here, 122 00:06:05,040 --> 00:06:06,780 it outputs the kind of header message, 123 00:06:06,780 --> 00:06:08,880 a demo of simple enums. 124 00:06:08,880 --> 00:06:11,400 I declare my enum variable, 125 00:06:11,400 --> 00:06:13,740 I test its type, and it's red, 126 00:06:13,740 --> 00:06:15,963 it outputs coch like so. 127 00:06:17,160 --> 00:06:18,660 Right, so those warnings, 128 00:06:18,660 --> 00:06:20,850 I'm gonna tidy my code up a bit later 129 00:06:20,850 --> 00:06:22,560 to get rid of those warnings, 130 00:06:22,560 --> 00:06:25,500 but for now we'll just kind of ignore them. 131 00:06:25,500 --> 00:06:27,630 A couple of things I wanted to say. 132 00:06:27,630 --> 00:06:30,420 First of all, what would happen... 133 00:06:30,420 --> 00:06:32,970 I tell you what I'm gonna do, actually, I'm gonna delete, 134 00:06:32,970 --> 00:06:36,810 I'm gonna temporarily delete these other functions, okay, 135 00:06:36,810 --> 00:06:38,550 because we don't need them just yet. 136 00:06:38,550 --> 00:06:39,840 We'll just have this simple function 137 00:06:39,840 --> 00:06:42,603 just to get rid of some of those warnings. 138 00:06:44,220 --> 00:06:49,220 Okay, so what if I didn't include the mod statement? 139 00:06:51,120 --> 00:06:53,460 Okay, let's comment that line out, 140 00:06:53,460 --> 00:06:56,100 and I'll comment this line out as well. 141 00:06:56,100 --> 00:06:59,640 So if you don't declare the module, 142 00:06:59,640 --> 00:07:00,930 then it never bothers 143 00:07:00,930 --> 00:07:04,140 processing the code in that module, okay? 144 00:07:04,140 --> 00:07:06,720 So this keyword, this here is required 145 00:07:06,720 --> 00:07:09,270 to kind of import the code. 146 00:07:09,270 --> 00:07:11,760 If I don't import the module, 147 00:07:11,760 --> 00:07:13,683 if I basically comment out line two, 148 00:07:15,030 --> 00:07:18,000 then I try to run it, cargo run, 149 00:07:18,000 --> 00:07:19,950 watch what error message I'm gonna get. 150 00:07:20,880 --> 00:07:24,930 It complains here on line 23, 151 00:07:24,930 --> 00:07:27,240 it says, "What is this color type? 152 00:07:27,240 --> 00:07:29,370 I've never seen it before." 153 00:07:29,370 --> 00:07:31,717 Okay, to which I might say as a programmer, 154 00:07:31,717 --> 00:07:36,683 "But I defined it over here, color enum." 155 00:07:37,710 --> 00:07:39,210 But the compiler would reply back to me 156 00:07:39,210 --> 00:07:40,350 because we're having a dialogue here, 157 00:07:40,350 --> 00:07:42,457 me and the compiler, the compiler would say, 158 00:07:42,457 --> 00:07:44,400 "Yeah, but you never told me 159 00:07:44,400 --> 00:07:47,040 to input that module, did you Andy?" 160 00:07:47,040 --> 00:07:48,750 And I would say, "Oh, thank you very much. 161 00:07:48,750 --> 00:07:50,910 That's very polite of you, dear compiler. 162 00:07:50,910 --> 00:07:53,130 Now I see what I've done wrong." 163 00:07:53,130 --> 00:07:55,500 Now I have imported that code, okay, 164 00:07:55,500 --> 00:07:58,230 so that code will now be processed. 165 00:07:58,230 --> 00:08:00,780 You have to actually import it like so. 166 00:08:00,780 --> 00:08:03,480 So, good, I've still got this line 167 00:08:03,480 --> 00:08:05,370 commented out though, notice. 168 00:08:05,370 --> 00:08:08,040 Okay, whoops. So that line is still 169 00:08:08,040 --> 00:08:10,470 not kind of being processed. 170 00:08:10,470 --> 00:08:11,640 So what would happen 171 00:08:11,640 --> 00:08:14,070 if I tried to run the application again? 172 00:08:14,070 --> 00:08:15,513 Well, another error. 173 00:08:16,710 --> 00:08:18,900 Now it's saying, what's it saying here, 174 00:08:18,900 --> 00:08:20,790 what's the first error message? 175 00:08:20,790 --> 00:08:25,200 Again it's saying, "Use of undeclared type color." 176 00:08:25,200 --> 00:08:29,760 So the problem is, a module is kind of like a namespace. 177 00:08:29,760 --> 00:08:32,310 When you declare a module at mytypes, 178 00:08:32,310 --> 00:08:37,310 this name is effectively in a namespace, mytypes, okay? 179 00:08:38,490 --> 00:08:40,500 So if you wanted to use it 180 00:08:40,500 --> 00:08:44,790 you'd have to qualify it by its module name. 181 00:08:44,790 --> 00:08:48,217 So what I could do is I could say, 182 00:08:48,217 --> 00:08:51,870 "Okay, everywhere I'm using the color type 183 00:08:51,870 --> 00:08:55,560 I need to tell it that that's part of the mytypes module," 184 00:08:55,560 --> 00:08:59,640 like a namespace, and the :: is like a scope operator. 185 00:08:59,640 --> 00:09:02,943 So go to this module, which is like a namespace, 186 00:09:02,943 --> 00:09:06,000 in there you'll find the color type, yes, 187 00:09:06,000 --> 00:09:08,790 and it has the red variant. 188 00:09:08,790 --> 00:09:13,170 That's good, but I'd have to use this prefix everywhere 189 00:09:13,170 --> 00:09:14,970 where I've referenced the type name, 190 00:09:15,930 --> 00:09:20,930 and already I'm starting to get a bit fed up with this, 191 00:09:22,170 --> 00:09:25,027 having to qualify it by its module name everywhere. 192 00:09:25,027 --> 00:09:27,270 "But that would actually work," he says. 193 00:09:27,270 --> 00:09:30,360 Let's that a quick run, that will actually work. 194 00:09:30,360 --> 00:09:33,090 Okay, so that has actually worked there. 195 00:09:33,090 --> 00:09:36,483 Couple of warnings but it has actually worked, okay. 196 00:09:37,530 --> 00:09:42,120 So, yeah, that's basically the idea of the use keyword, 197 00:09:42,120 --> 00:09:47,120 rather than having to fully qualify color by its typing 198 00:09:47,160 --> 00:09:48,930 you can bring it into scope. 199 00:09:48,930 --> 00:09:53,310 The use keyword basically brings that symbol name 200 00:09:53,310 --> 00:09:55,320 into top level scope, 201 00:09:55,320 --> 00:09:57,870 so the color symbol is now, from mytypes, 202 00:09:57,870 --> 00:09:59,970 is now directly in scope. 203 00:09:59,970 --> 00:10:03,780 So it's kind of almost like a global symbol name now 204 00:10:03,780 --> 00:10:06,150 in my code, and I don't any longer 205 00:10:06,150 --> 00:10:08,010 need to use the mytypes prefix, 206 00:10:08,010 --> 00:10:11,640 it's already been dragged into scope by the use keyword. 207 00:10:11,640 --> 00:10:15,840 So the mod keyword actually physically imports the code file 208 00:10:15,840 --> 00:10:19,470 and the used keyword introduces a name into scope, 209 00:10:19,470 --> 00:10:21,390 so you can just use that name directly 210 00:10:21,390 --> 00:10:25,440 without attempt to prefix by its module name, like so. 211 00:10:25,440 --> 00:10:29,430 Okay, so that hopefully explains the mod keyword 212 00:10:29,430 --> 00:10:33,720 to import a module, to basically drag the code in, 213 00:10:33,720 --> 00:10:35,850 and the use keyword to give you 214 00:10:35,850 --> 00:10:37,680 direct access to those names 215 00:10:37,680 --> 00:10:40,770 directly in the current scope of your current file, 216 00:10:40,770 --> 00:10:44,223 without the name, for the module name as a prefix anywhere. 217 00:10:45,660 --> 00:10:48,750 Okay, I feel like I should run it again. 218 00:10:48,750 --> 00:10:52,920 So it's working again and it outputs coch. 219 00:10:52,920 --> 00:10:53,880 Couple of other things, 220 00:10:53,880 --> 00:10:55,930 which I'll just finish off by mentioning. 221 00:10:56,880 --> 00:11:01,350 Here, when you declare a variant, a variable, sorry, 222 00:11:01,350 --> 00:11:05,100 and you give it an enum variant, 223 00:11:05,100 --> 00:11:08,430 you don't really need to type it. 224 00:11:08,430 --> 00:11:11,100 You don't need to specify it's a color, 225 00:11:11,100 --> 00:11:13,350 it can guess that it's a color. 226 00:11:13,350 --> 00:11:14,940 By virtue of the fact that you've given it 227 00:11:14,940 --> 00:11:18,600 one of the color variants, it knows that it's a color. 228 00:11:18,600 --> 00:11:21,213 Okay, so that would actually be a little bit better. 229 00:11:22,470 --> 00:11:24,393 Okay, so that is working still. 230 00:11:25,320 --> 00:11:27,780 And just one last thing, 231 00:11:27,780 --> 00:11:31,230 you still need to specify the enum type 232 00:11:31,230 --> 00:11:34,590 in front of the variant, he can't do that, okay, 233 00:11:34,590 --> 00:11:37,020 because red, green and blue 234 00:11:37,020 --> 00:11:41,910 might, for example, be variants on another enum type. 235 00:11:41,910 --> 00:11:45,930 You might have like a political party in the UK, 236 00:11:45,930 --> 00:11:49,110 green is a political party in the UK. 237 00:11:49,110 --> 00:11:52,620 So that wouldn't work, you're gonna get an error here. 238 00:11:52,620 --> 00:11:57,543 If I run the compiler again it will say, 239 00:11:58,687 --> 00:12:00,840 "I don't understand what you're talking about here, 240 00:12:00,840 --> 00:12:05,610 I've lost it. This symbol name is not understood." 241 00:12:05,610 --> 00:12:09,393 Okay, oh, right, so basically I need to scope it like so. 242 00:12:11,100 --> 00:12:13,980 Okay, so the actual process 243 00:12:13,980 --> 00:12:16,380 of defining enum types is quite straightforward, 244 00:12:16,380 --> 00:12:20,040 remember to make it pub so it's kind of visible elsewhere. 245 00:12:20,040 --> 00:12:23,850 The variants are always public when you have an enum. 246 00:12:23,850 --> 00:12:25,710 The interesting part, really, 247 00:12:25,710 --> 00:12:28,740 most of our discussion here is about organizing our code. 248 00:12:28,740 --> 00:12:31,770 I've put it into a module file so it can be imported 249 00:12:31,770 --> 00:12:34,260 in lots of other applications, potentially. 250 00:12:34,260 --> 00:12:38,010 In my main code, I input that module, 251 00:12:38,010 --> 00:12:40,110 technically I'm declaring the module, 252 00:12:40,110 --> 00:12:43,890 I'm dragging that module file into my application, 253 00:12:43,890 --> 00:12:48,150 I'm introducing these names directly into scope 254 00:12:48,150 --> 00:12:50,673 so I can use them without qualification. 255 00:12:51,630 --> 00:12:53,940 Right, okay, so I think there's one other thing 256 00:12:53,940 --> 00:12:55,290 I want to discuss, 257 00:12:55,290 --> 00:12:57,483 let me just run the compiler one last time. 258 00:12:59,037 --> 00:13:02,454 (keyboard keys clacking) 259 00:13:03,600 --> 00:13:07,560 Note this, I'm still getting some warnings. 260 00:13:07,560 --> 00:13:09,510 I'm gonna look at this one in here. 261 00:13:09,510 --> 00:13:14,510 It sees my color enum and my color enum has three variants, 262 00:13:15,180 --> 00:13:16,830 red, green, and blue, 263 00:13:16,830 --> 00:13:18,540 and it's complaining 264 00:13:18,540 --> 00:13:23,540 that I'm not using the green or the blue variant. 265 00:13:23,550 --> 00:13:24,930 Technically they never constructed, 266 00:13:24,930 --> 00:13:28,500 it means my code never seems to make use of color blue. 267 00:13:28,500 --> 00:13:30,300 I'm not actually creating a variable 268 00:13:30,300 --> 00:13:32,100 of color blue or color green, 269 00:13:32,100 --> 00:13:34,200 obviously because it's such a small example 270 00:13:34,200 --> 00:13:36,060 I don't need to. 271 00:13:36,060 --> 00:13:39,240 In a large application, hopefully all of these enums, 272 00:13:39,240 --> 00:13:40,170 all of these variants 273 00:13:40,170 --> 00:13:43,050 would kind of crop up somewhere in my code. 274 00:13:43,050 --> 00:13:44,970 At the moment, it's complaining 275 00:13:44,970 --> 00:13:48,127 that there are variants here that aren't being used, 276 00:13:48,127 --> 00:13:50,850 "Why are you declaring these variants 277 00:13:50,850 --> 00:13:52,830 if you're not using them?" 278 00:13:52,830 --> 00:13:55,140 Okay, so the solution to that dilemma 279 00:13:55,140 --> 00:13:57,140 we're gonna look at in the next section.