Rust Enums

In the Rust programming language, an enum is a custom data type that is comprised of variants.

The term ‘enum’ is short for enumeration. In essence, an enum is a set of named values called variants. When we declare an enum, we provide the list of possible variants. When we later initialize an enum instance, that instance must be one of the variants.

For example, we can create an enum called Color. Let’s say that Color has three variants: red, green, and blue. In code, this can be written as follows:

enum Color {
    Red,
    Green,
    Blue,
}

We can create an instance of the Color enum as follows:

let my_favorite_color = Color::Green;
println!("My favorite color is: {:?}", my_favorite_color);

Standard Output:

My favorite color is: Green

Enums are useful because they allow us to encode meaning as well as information.

They are generally used when we want to reference something that is of one type but not another. In the above example, my favorite color is Green, not Blue. We can contrast this with structs, a custom data type comprised of multiple elements.

In this tutorial, we will cover the essentials of Rust enums.

Declaring an Enum

An enum is declared using the keyword enum followed by the name and curly braces containing the variants:

enum EnumName {
    Variant1,
    Variant2,
    Variant3,
}

Note that the curly braces essentially contain a list of variants separated by a comma. When we initialize an enum, it must be one of that enum’s variants.

Rust Enum Naming Convention

In Rust, enums and enum variants are both named using UpperCamelCase, also called PascalCase. This means that the first letter of each word is capitalized and words are not separated by characters.

In the following example, we are using an enum to define a player’s possible movements in a video game. The name of the enum is PlayerMove, and the variants are Up, Down, Left, and Right.

enum PlayerMove {
    Up,
    Down,
    Left,
    Right,
}

Initializing an Enum

Enums are initialized by creating an instance of the enum, which is assigned to a variable. To initialize an enum, we use the enum name followed by two colons ‘::’ and then the enum variant:

let my_enum = EnumName::VariantName;

Returning to our PlayerMove example, let’s initialize a variable for each variant that allows us to work with our enum:

enum PlayerMove {
    Up,
    Down,
    Left,
    Right,
}

let up_move = PlayerMove::Up;
let down_move = PlayerMove::Down;
let left_move = PlayerMove::Left;
let right_move = PlayerMove::Right;

Now when we want our player to move, we can use the variable that corresponds to the move variant.

Enums With a Data Type

Until this point, we have allowed the Rust compiler to implicitly determine the data types of an enum’s variants.

We can also explicitly include the variant type in the enum declaration:

enum EnumName {
    Variant1(type),
    Variant2(type),
    Variant3(type),
}

When we initialize the enum, we can specify data of the specified type to be returned when the corresponding variable is called.

enum PlayerMove {
    Up(String),
    Down(String),
    Left(String),
    Right(String),
}

let up_move = PlayerMove::Up("Player Moves Up".to_string());
let down_move = PlayerMove::Down("Player Moves Down".to_string());
let left_move = PlayerMove::Left("Player Moves Left".to_string());
let right_move = PlayerMove::Right("Player Moves Right".to_string());