1 00:00:06,600 --> 00:00:09,570 - Let's imagine we define a simple enum type 2 00:00:09,570 --> 00:00:10,440 like we have here. 3 00:00:10,440 --> 00:00:12,360 I've defined an enum type called color. 4 00:00:12,360 --> 00:00:13,750 It has a couple of variants, 5 00:00:13,750 --> 00:00:16,050 well, three, red, green, and blue. 6 00:00:16,050 --> 00:00:17,940 And let's say in my code 7 00:00:17,940 --> 00:00:20,040 I use some of those variants, but not all of them. 8 00:00:20,040 --> 00:00:21,390 I use the red variant 9 00:00:21,390 --> 00:00:24,030 but let's say I don't use the green variant 10 00:00:24,030 --> 00:00:25,920 and I don't use the blue variant. 11 00:00:25,920 --> 00:00:28,560 Well, we saw this earlier. 12 00:00:28,560 --> 00:00:31,140 If there are variants that you haven't used anywhere 13 00:00:31,140 --> 00:00:34,200 in your code, the Rust compiler gives you a warning message 14 00:00:34,200 --> 00:00:35,730 about dead code. 15 00:00:35,730 --> 00:00:38,910 It'll say the green variant isn't used. 16 00:00:38,910 --> 00:00:40,380 Why have we defined it? 17 00:00:40,380 --> 00:00:41,760 The blue variant isn't used. 18 00:00:41,760 --> 00:00:43,500 Why have we defined that? 19 00:00:43,500 --> 00:00:45,360 So we have seen this example earlier. 20 00:00:45,360 --> 00:00:48,303 If you go back to the project lesson four enums, 21 00:00:49,140 --> 00:00:53,400 in mytypes.rs, I defined a color enum type. 22 00:00:53,400 --> 00:00:58,290 In main.rs, I had a function called demo_simple_enums. 23 00:00:58,290 --> 00:01:00,450 It used the red variant 24 00:01:00,450 --> 00:01:03,840 but it didn't use the green or the blue variant. 25 00:01:03,840 --> 00:01:06,900 When I run the application, I get warnings. 26 00:01:06,900 --> 00:01:07,980 So let me just remind you 27 00:01:07,980 --> 00:01:09,210 what those warnings look like 28 00:01:09,210 --> 00:01:11,730 and then we're gonna see how to fix them. 29 00:01:11,730 --> 00:01:14,010 So here's the code for lesson four. 30 00:01:14,010 --> 00:01:17,070 And remember mytypes.rs, 31 00:01:17,070 --> 00:01:21,240 it defined a couple of enums, a color enum, house location. 32 00:01:21,240 --> 00:01:23,970 We have a look at that later on in the lesson. 33 00:01:23,970 --> 00:01:28,290 Let's focus on the color enum for now. 34 00:01:28,290 --> 00:01:33,290 Three variants, in my main code, I import the module, 35 00:01:35,130 --> 00:01:37,290 I introduce the color enum 36 00:01:37,290 --> 00:01:40,140 into scope plus house location. 37 00:01:40,140 --> 00:01:44,593 In my main code when I call demo_simple_enums, here it is, 38 00:01:46,860 --> 00:01:48,810 in this function I just used the red enum. 39 00:01:48,810 --> 00:01:52,110 I declared a variable and used the red variant 40 00:01:52,110 --> 00:01:53,700 but not the green or the blue. 41 00:01:53,700 --> 00:01:57,450 I'm testing for them, but I haven't actually used, 42 00:01:57,450 --> 00:02:00,120 I haven't actually assigned the value to a variable. 43 00:02:00,120 --> 00:02:01,650 And that's the critical thing. 44 00:02:01,650 --> 00:02:05,190 So let me open the terminal window and I'm gonna remind you 45 00:02:05,190 --> 00:02:07,350 about the warnings that we're gonna get. 46 00:02:07,350 --> 00:02:08,580 We're gonna get lots of warnings here 47 00:02:08,580 --> 00:02:11,283 about unused code, cargo run. 48 00:02:14,220 --> 00:02:17,210 So we had from the top, 49 00:02:17,210 --> 00:02:18,690 and I talked about this a bit earlier, 50 00:02:18,690 --> 00:02:20,580 we had lots of functions 51 00:02:20,580 --> 00:02:23,043 that exist that haven't been called, 52 00:02:24,120 --> 00:02:28,110 but also we have the color enum. 53 00:02:28,110 --> 00:02:30,000 The green variant isn't used 54 00:02:30,000 --> 00:02:32,610 and the blue variant isn't used either, okay? 55 00:02:32,610 --> 00:02:37,023 So why do they exist if I never, ever use them? 56 00:02:38,400 --> 00:02:41,040 So I mean it's a legitimate question really, 57 00:02:41,040 --> 00:02:44,370 but maybe it's because you haven't got round to it yet 58 00:02:44,370 --> 00:02:47,970 and you would just like the compiler to stop complaining. 59 00:02:47,970 --> 00:02:52,970 So what you can do is you can use an attribute, this syntax, 60 00:02:54,270 --> 00:02:59,270 a hash symbol or whatever you call it, a hash symbol 61 00:02:59,430 --> 00:03:01,590 and then square brackets. 62 00:03:01,590 --> 00:03:05,430 This is called an attribute in Rust. 63 00:03:05,430 --> 00:03:08,280 And it's kind of like a compiler directive. 64 00:03:08,280 --> 00:03:11,850 And if you use the allow directive, you can specify 65 00:03:11,850 --> 00:03:14,850 or the allow attribute, you can specify what kind 66 00:03:14,850 --> 00:03:17,400 of language features you want to allow 67 00:03:17,400 --> 00:03:19,170 without giving warnings. 68 00:03:19,170 --> 00:03:22,320 By default, it would give you warnings for dead code 69 00:03:22,320 --> 00:03:23,970 but you can use this attribute 70 00:03:23,970 --> 00:03:27,300 to allow dead code in your application, okay? 71 00:03:27,300 --> 00:03:30,930 So you're not gonna get warnings from the compiler anymore. 72 00:03:30,930 --> 00:03:35,220 So what you can do is you can qualify each variant 73 00:03:35,220 --> 00:03:38,310 like this and you would have to do it for each variant. 74 00:03:38,310 --> 00:03:42,240 You can say, in my color enum, the red variant, 75 00:03:42,240 --> 00:03:45,990 I'm going to allow that red variant not to be used, 76 00:03:45,990 --> 00:03:47,760 and the green and the blue, 77 00:03:47,760 --> 00:03:52,760 you label each variant to allow it not to be used 78 00:03:53,130 --> 00:03:54,713 in your application, okay? 79 00:03:56,040 --> 00:03:58,980 So we're gonna have a look at how to do that 80 00:03:58,980 --> 00:04:01,380 in the application and see how it helps matters. 81 00:04:01,380 --> 00:04:05,580 We'll define an enum that does allow variants not 82 00:04:05,580 --> 00:04:09,420 to be used, and we'll see what happens when we run it. 83 00:04:09,420 --> 00:04:13,470 The warnings disappear, so let's do that. 84 00:04:13,470 --> 00:04:17,973 It's in my other file, mytypes_for_demo_purposes. 85 00:04:18,990 --> 00:04:22,110 I spent age years thinking of the name for this module. 86 00:04:22,110 --> 00:04:25,620 So the module name is mytypes_for_demo_purposes. 87 00:04:25,620 --> 00:04:26,940 I've defined a different color. 88 00:04:26,940 --> 00:04:30,090 This color enum type is in this module, 89 00:04:30,090 --> 00:04:31,650 namespace, if you like. 90 00:04:31,650 --> 00:04:36,090 It is different from the color type defined in here. 91 00:04:36,090 --> 00:04:39,150 The module name is like a namespace. 92 00:04:39,150 --> 00:04:41,730 So technically this enum 93 00:04:41,730 --> 00:04:46,350 is of type mytypes colon colon color. 94 00:04:46,350 --> 00:04:51,350 And this enum type is of type mytypes_for_demo_purposes, 95 00:04:55,230 --> 00:04:56,910 colon colon color, 96 00:04:56,910 --> 00:05:00,000 it is a different type name in a different scope. 97 00:05:00,000 --> 00:05:05,000 And in this enum, I'm allowing the variants not to be used. 98 00:05:05,310 --> 00:05:08,100 I'm allowing these to be dead code. 99 00:05:08,100 --> 00:05:10,590 And I've done it for house location as well. 100 00:05:10,590 --> 00:05:14,310 So in my main code, I'm going to use this module, 101 00:05:14,310 --> 00:05:16,860 I'm gonna import this module instead 102 00:05:16,860 --> 00:05:19,950 of importing the previous one. 103 00:05:19,950 --> 00:05:23,100 So in my main code right at the top 104 00:05:23,100 --> 00:05:26,370 I will comment these statements out. 105 00:05:26,370 --> 00:05:29,433 I'm no longer gonna use mytypes module. 106 00:05:30,300 --> 00:05:32,400 Instead I'm going to use 107 00:05:32,400 --> 00:05:36,810 the mytypes_for_demo_purposes module, input that file, 108 00:05:36,810 --> 00:05:39,870 and also from that file or from that module, 109 00:05:39,870 --> 00:05:41,880 introduce those enums into scope. 110 00:05:41,880 --> 00:05:46,880 So these enums now will not complain about unused variants. 111 00:05:48,270 --> 00:05:50,370 So let's give that a try. 112 00:05:50,370 --> 00:05:52,233 Let's cargo run again. 113 00:05:53,760 --> 00:05:56,250 I'm hoping there will not be any warnings now 114 00:05:56,250 --> 00:05:57,903 about unused variants. 115 00:05:59,880 --> 00:06:04,170 I'm still getting warnings about unused functions. 116 00:06:04,170 --> 00:06:07,080 Okay, so that's a, you know, rather disappointing. 117 00:06:07,080 --> 00:06:09,663 I've gotta say, not unexpected. 118 00:06:10,560 --> 00:06:15,360 I'm not getting any warnings about variants being unused. 119 00:06:15,360 --> 00:06:16,770 So that was good. 120 00:06:16,770 --> 00:06:19,560 Now what I could do is I could use the same technique 121 00:06:19,560 --> 00:06:22,680 for the functions, I could say I've got this function, 122 00:06:22,680 --> 00:06:24,480 I will eventually be calling it 123 00:06:24,480 --> 00:06:28,380 when we get there in the lesson later on, but not yet. 124 00:06:28,380 --> 00:06:33,180 So what I could do is I could pinch this attribute 125 00:06:33,180 --> 00:06:35,310 and you can use it to qualify anything. 126 00:06:35,310 --> 00:06:39,990 You can qualify a variant to say allow this not to be used. 127 00:06:39,990 --> 00:06:42,960 You can also use it to qualify functions. 128 00:06:42,960 --> 00:06:45,390 So you could put this attribute 129 00:06:45,390 --> 00:06:50,390 at the beginning of each function, like so, okay? 130 00:06:50,730 --> 00:06:54,540 Other languages have similar kind of features 131 00:06:54,540 --> 00:06:57,270 and this is how you can do it in Rust. 132 00:06:57,270 --> 00:07:00,240 Obviously if you are using this too much 133 00:07:00,240 --> 00:07:01,530 you're gonna start wondering 134 00:07:01,530 --> 00:07:05,490 why does this function even exist if I'm not calling it. 135 00:07:05,490 --> 00:07:08,280 So you are kind of suppressing a warning 136 00:07:08,280 --> 00:07:10,470 that was actually something you should have taken notice of 137 00:07:10,470 --> 00:07:12,060 but you can do it. 138 00:07:12,060 --> 00:07:14,943 So if I compile now, let's see what happens. 139 00:07:15,960 --> 00:07:19,590 Beautiful, free of any warnings, 140 00:07:19,590 --> 00:07:21,780 because I'm allowing dead code. 141 00:07:21,780 --> 00:07:24,750 This is dead code, it's never called, 142 00:07:24,750 --> 00:07:26,433 I'm gonna allow dead code. 143 00:07:29,520 --> 00:07:31,590 The only caveat is that it's a bit tiresome 144 00:07:31,590 --> 00:07:36,590 having to qualify every single thing to allow dead code. 145 00:07:38,220 --> 00:07:40,470 Every single thing in my application 146 00:07:40,470 --> 00:07:43,200 I could qualify individually like that. 147 00:07:43,200 --> 00:07:45,093 There is another way to do it. 148 00:07:45,990 --> 00:07:50,070 You can define an attribute at the top of your application. 149 00:07:50,070 --> 00:07:52,953 It's a slightly different syntax, it looks like this. 150 00:07:53,820 --> 00:07:55,200 At the top of your application, 151 00:07:55,200 --> 00:07:58,650 you say you have an exclamation mark here, okay? 152 00:07:58,650 --> 00:08:02,340 Technically your entire application 153 00:08:02,340 --> 00:08:04,860 in Rust is called a crate, okay? 154 00:08:04,860 --> 00:08:09,860 So this is a crate-scope attribute. 155 00:08:10,290 --> 00:08:14,190 It applies across the entire crate. 156 00:08:14,190 --> 00:08:16,560 The crate, all the terms in Rust 157 00:08:16,560 --> 00:08:18,030 are something to do with crabs, 158 00:08:18,030 --> 00:08:20,820 like crustaceans, Rustaceans. 159 00:08:20,820 --> 00:08:24,420 A crate is something that would contain crabs. 160 00:08:24,420 --> 00:08:29,420 So in Rust terminology, your entire application is a crate 161 00:08:29,760 --> 00:08:32,880 and this is a crate scope attribute 162 00:08:32,880 --> 00:08:36,180 or rather I should call it a crate level attribute. 163 00:08:36,180 --> 00:08:39,480 It applies across the entire application. 164 00:08:39,480 --> 00:08:42,300 Allow dead code anywhere in my application. 165 00:08:42,300 --> 00:08:45,690 So technically now I could revert back. 166 00:08:45,690 --> 00:08:50,580 If I comment those out and uncomment my original files, 167 00:08:50,580 --> 00:08:54,330 like so, then it will allow dead code 168 00:08:54,330 --> 00:08:56,760 without complaining, very nice. 169 00:08:56,760 --> 00:08:57,813 When I input mytypes, 170 00:08:58,920 --> 00:09:01,830 even though there are some variants here which aren't used, 171 00:09:01,830 --> 00:09:04,530 it'll say, aha, but I'm going to allow that. 172 00:09:04,530 --> 00:09:07,560 So this is actually the easiest way 173 00:09:07,560 --> 00:09:10,863 to avoid warnings everywhere about unused code. 174 00:09:12,030 --> 00:09:15,060 So fingers crossed, if I run the application again, 175 00:09:15,060 --> 00:09:17,523 it will give me no warnings. 176 00:09:20,469 --> 00:09:21,302 I love it.