- Let's imagine we define a simple enum type
like we have here.
I've defined an enum type called color.
It has a couple of variants,
well, three, red, green, and blue.
And let's say in my code
I use some of those variants, but not all of them.
I use the red variant
but let's say I don't use the green variant
and I don't use the blue variant.
Well, we saw this earlier.
If there are variants that you haven't used anywhere
in your code, the Rust compiler gives you a warning message
about dead code.
It'll say the green variant isn't used.
Why have we defined it?
The blue variant isn't used.
Why have we defined that?
So we have seen this example earlier.
If you go back to the project lesson four enums,
in mytypes.rs, I defined a color enum type.
In main.rs, I had a function called demo_simple_enums.
It used the red variant
but it didn't use the green or the blue variant.
When I run the application, I get warnings.
So let me just remind you
what those warnings look like
and then we're gonna see how to fix them.
So here's the code for lesson four.
And remember mytypes.rs,
it defined a couple of enums, a color enum, house location.
We have a look at that later on in the lesson.
Let's focus on the color enum for now.
Three variants, in my main code, I import the module,
I introduce the color enum
into scope plus house location.
In my main code when I call demo_simple_enums, here it is,
in this function I just used the red enum.
I declared a variable and used the red variant
but not the green or the blue.
I'm testing for them, but I haven't actually used,
I haven't actually assigned the value to a variable.
And that's the critical thing.
So let me open the terminal window and I'm gonna remind you
about the warnings that we're gonna get.
We're gonna get lots of warnings here
about unused code, cargo run.
So we had from the top,
and I talked about this a bit earlier,
we had lots of functions
that exist that haven't been called,
but also we have the color enum.
The green variant isn't used
and the blue variant isn't used either, okay?
So why do they exist if I never, ever use them?
So I mean it's a legitimate question really,
but maybe it's because you haven't got round to it yet
and you would just like the compiler to stop complaining.
So what you can do is you can use an attribute, this syntax,
a hash symbol or whatever you call it, a hash symbol
and then square brackets.
This is called an attribute in Rust.
And it's kind of like a compiler directive.
And if you use the allow directive, you can specify
or the allow attribute, you can specify what kind
of language features you want to allow
without giving warnings.
By default, it would give you warnings for dead code
but you can use this attribute
to allow dead code in your application, okay?
So you're not gonna get warnings from the compiler anymore.
So what you can do is you can qualify each variant
like this and you would have to do it for each variant.
You can say, in my color enum, the red variant,
I'm going to allow that red variant not to be used,
and the green and the blue,
you label each variant to allow it not to be used
in your application, okay?
So we're gonna have a look at how to do that
in the application and see how it helps matters.
We'll define an enum that does allow variants not
to be used, and we'll see what happens when we run it.
The warnings disappear, so let's do that.
It's in my other file, mytypes_for_demo_purposes.
I spent age years thinking of the name for this module.
So the module name is mytypes_for_demo_purposes.
I've defined a different color.
This color enum type is in this module,
namespace, if you like.
It is different from the color type defined in here.
The module name is like a namespace.
So technically this enum
is of type mytypes colon colon color.
And this enum type is of type mytypes_for_demo_purposes,
colon colon color,
it is a different type name in a different scope.
And in this enum, I'm allowing the variants not to be used.
I'm allowing these to be dead code.
And I've done it for house location as well.
So in my main code, I'm going to use this module,
I'm gonna import this module instead
of importing the previous one.
So in my main code right at the top
I will comment these statements out.
I'm no longer gonna use mytypes module.
Instead I'm going to use
the mytypes_for_demo_purposes module, input that file,
and also from that file or from that module,
introduce those enums into scope.
So these enums now will not complain about unused variants.
So let's give that a try.
Let's cargo run again.
I'm hoping there will not be any warnings now
about unused variants.
I'm still getting warnings about unused functions.
Okay, so that's a, you know, rather disappointing.
I've gotta say, not unexpected.
I'm not getting any warnings about variants being unused.
So that was good.
Now what I could do is I could use the same technique
for the functions, I could say I've got this function,
I will eventually be calling it
when we get there in the lesson later on, but not yet.
So what I could do is I could pinch this attribute
and you can use it to qualify anything.
You can qualify a variant to say allow this not to be used.
You can also use it to qualify functions.
So you could put this attribute
at the beginning of each function, like so, okay?
Other languages have similar kind of features
and this is how you can do it in Rust.
Obviously if you are using this too much
you're gonna start wondering
why does this function even exist if I'm not calling it.
So you are kind of suppressing a warning
that was actually something you should have taken notice of
but you can do it.
So if I compile now, let's see what happens.
Beautiful, free of any warnings,
because I'm allowing dead code.
This is dead code, it's never called,
I'm gonna allow dead code.
The only caveat is that it's a bit tiresome
having to qualify every single thing to allow dead code.
Every single thing in my application
I could qualify individually like that.
There is another way to do it.
You can define an attribute at the top of your application.
It's a slightly different syntax, it looks like this.
At the top of your application,
you say you have an exclamation mark here, okay?
Technically your entire application
in Rust is called a crate, okay?
So this is a crate-scope attribute.
It applies across the entire crate.
The crate, all the terms in Rust
are something to do with crabs,
like crustaceans, Rustaceans.
A crate is something that would contain crabs.
So in Rust terminology, your entire application is a crate
and this is a crate scope attribute
or rather I should call it a crate level attribute.
It applies across the entire application.
Allow dead code anywhere in my application.
So technically now I could revert back.
If I comment those out and uncomment my original files,
like so, then it will allow dead code
without complaining, very nice.
When I input mytypes,
even though there are some variants here which aren't used,
it'll say, aha, but I'm going to allow that.
So this is actually the easiest way
to avoid warnings everywhere about unused code.
So fingers crossed, if I run the application again,
it will give me no warnings.
I love it.