>> In this video,
let's use Visual Studio Code to read
a couple different enum definitions.
I'll start by creating a new project
called enums over in my terminal now.
Once at my terminal in the Rust Projects directory,
I'm going to type cargo new enums.
Then once that enums package is created,
I will change directories to
enums and open that up in Visual Studio Code.
Once I'm in Visual Studio Code,
I'll focus my attention on the main.rs program.
Above the main function,
I'll start by defining an enum to classify a web event.
This enum will be called
WebEvent and have multiple different variance.
First, there's going to be PageLoad and PageUnload.
Notice that these variants have
no data associated with them at all,
these are unit like.
Next, I will have a KeyPress variant,
which takes in a single character.
This is like a tuple struct.
Next up, I'll have Paste,
which will allow a string value.
Then finally, I'll have
a Click variant which will include an anonymous struct,
including the types x i64 and y i64.
In this example, you can get a better picture for
how flexible enums truly are,
with being able to group lots of different types of data together.
When I want to use or access a variant from the WebEvent enum,
I can do the following: inside of the main function,
I'll get rid of the println line and go
ahead and define a new variable that I'll call quit.
Then I will call the WebEvent with the KeyPress variant.
For the character, I want to pass in
q to indicate that that will quit the program.
That's a simple way of how I can
define one variable to access a variant.
A similar convention can be followed when setting
the value of other variance to variables.
Another type of enum to explore is the option enum.
This enum is defined by the Standard Library.
The option type is used in many places because it
captures the scenarios in which
a value can either be something or nothing.
Rust doesn't have null values but it does have an enum to
encode the concept and the value being either null or not null.
The option enum is defined by the Standard Library as follows:
I use the enum keyword and specify the name of the enum,
which is option with a perimeter of T,
which is a generic trait,
which we'll talk about later.
Then for the variance of this enum,
I will have Some which takes the type T,
and then I will have None as the second variant there.
What's interesting about the option enum is that it's included in
the prelude and it's not necessary
to bring it into scope explicitly,
so the variance Some and None can be used
directly without the option prefix.
Again, the T syntax that you see here and
in the Some variant is a generic type parameter.
What's important to know is that T
means that it can hold some value,
that's one piece of data of any type.
Let's explore how to use the option enum now.
I can do the following if I want to
access the Some variant of the enum option.
I can define a variable called something is equal to Some 1.
This is how I can go ahead and use
the Some variant of the option enum and just pass in some value,
which here is type i32.
Using the option enum is helpful to prevent
common issues and handling
that scenario before encountering an error.
Not having to worry about incorrectly assuming
a not null value helps you to be more confident in your code.
There you saw a couple of examples of enums in action.
Enums are commonly used in program so it's important to get
familiar with understanding how to implement and use them.
[MUSIC]