- An enum is a data type
that has a closed set of allowed values.
So for example here,
a flight enum type might allow you to book a ticket
in economy class, business class, or first class,
but nothing else.
It's a closed set of allowed values
like a finite state machine
and we could use integers to achieve the same effect
like an integer that could be zero
to represent the economy class,
one to represent business class,
two to represent first class,
but enums are more self-describing.
They have a name and they only allow you to assign
one of the close set of values.
So it turns out the enums are really important in Rust.
There are enums that are defined in the language itself
and there are enums that we define in our own code.
So we'll have a look at both techniques.
First of all, we'll see how to define
our own enum types, quite simple.
Then later on in this lesson,
we'll see how to use some of the existing types,
enum types are defined in the language.
So to define an enum type is quite straightforward.
There's a keyword called enum.
You give your enum type a name
starting with a capital letter.
Rust is quite fussy about that.
And then you have curly brackets.
And then inside the curly brackets,
you basically list all the allowed possible values
for that enum type.
And those allowed values are called variants in Rust.
Other languages call them mnemonics or fields or states,
but the word that Rust uses is variant.
So here's an example.
I've got a simple enum type called Color.
It has three variants,
three allowed values: red, green, and blue.
You can only assign the value red, green, or blue
to a Color variable.
So you would declare the enum type in one part of your code
and then you could use it elsewhere.
So to use an enum type, again, it's quite straightforward.
You say the enum type, like color,
a couple of colons, double colon,
it's like a scope operator,
followed by the variant you want.
And then you typically use a match statement
to test the value.
Okay, so you could use an if statement,
but a match statement actually works quite well.
It looks like this.
In this example, I've declared an enum variable c,
c is of type Color,
so I can only assign it one of the allowed values,
red, green, or blue.
If I decided or tried to assign anything else,
I get an error.
And you do need to specify the enum type here.
You couldn't just say red.
You gotta say enum type colon colon red.
It is properly scoped.
And then you test the value for c.
If it is a value red, then output coch,
coch is the Welsh word for red.
If it's green, it outputs gwyrdd.
That's the Welsh word for green.
Or if it's blue, you output glas.
That's the Welsh word for blue.
Okay, so quite straightforward really.
What you probably do in practice
is to put enums into a separate file.
We haven't really talked
about multi-file projects yet in Rust,
so we're gonna start doing that now.
A Rust project can have lots of files.
The main RS file is obviously the entry point,
but you can have other files as well kind of collaborating
and those other files are called modules or module files.
So here I define a module file called mytypes.rs.
You can call it anything you want.
I've declared my Color enum type in this module file.
By default, when you define classes and enums
and functions and such in a separate module file,
by default, it's private to that file
which means you can only use it in that file.
To be able to use it in a different file,
you have to use the pub keyword.
Pub is the keyword that means public in Rust.
Okay, so I'm declaring my enum Color as being a public enum.
I can use it in other files.
The variants are implicitly public anyway
so as long as the enum type itself is public,
then the variants are also public,
meaning you can access the red, green, and blue values
in a different file.
So you would declare your enum type in a file, mytypes.rs,
and then you've gotta import this file into your main code.
Okay, so in your main code,
you load the module into main.rs.
You use the mod keyword.
When you use the mod keyword like this
and you just give it basically the name of the file
but without the .rs extension,
it's a bit like the way you import modules in Python.
Similar-ish to that.
It will look for a file called mytypes.rs
and it will import this code into your application.
You have to import the module
in order for this file to be noticed.
If you just declared this file
and then you forgot to declare it as a module,
it wouldn't load it.
It would just ignore this file altogether.
You have to explicitly load the My Types module
into your main application.
Once you've done that,
you can then use an alias if you like
or introduce the My Types color name into scope.
The use keyword brings a symbol name
directly into scope in your current file.
Okay, so first of all, import the module.
So the module is kind of processed,
that code now takes effect.
And then inside My Types, there's a color enum.
Bring the color enum directly into scope.
I can now use the color enum type directly in my code here.
I'm gonna run an example in a minute
and I'm gonna show various different combinations
to show what would happen if you didn't have the statement
or if you didn't have that statement
so you can kind of understand what the effect actually is.