Rust Lesson 19 – Enums | Dataplexa

Enums in Rust

In this lesson, you will learn about enums in Rust. Enums allow you to define a type by listing all possible values it can take.

Enums are extremely powerful in Rust and are used extensively for control flow, error handling, and data modeling.


What Is an Enum?

An enum (short for enumeration) is a data type that represents one value from a fixed set of possible variants.

Each variant can optionally store data.


Defining an Enum

You define an enum using the enum keyword.

enum Direction {
    North,
    South,
    East,
    West,
}

This enum can have only one of the four possible directions at a time.


Using Enum Variants

You create an enum value by specifying the enum name followed by the variant.

let move1 = Direction::North;
let move2 = Direction::West;

Enums with Associated Data

Enum variants can store additional data of different types.

enum Message {
    Quit,
    Write(String),
    Move(i32, i32),
    ChangeColor(i32, i32, i32),
}

Each variant can hold different kinds of values.


Creating Enum Values with Data

You can create enum values and pass data directly to the variants.

let msg1 = Message::Write(String::from("Hello"));
let msg2 = Message::Move(10, 20);
let msg3 = Message::ChangeColor(255, 0, 0);

Enums vs Structs

Structs group related fields together. Enums represent a value that can be one of many possible variants.

Enums are ideal when data can exist in different forms.


Enums and Methods

You can define methods for enums using impl.

impl Message {
    fn call(&self) {
        println!("Message received");
    }
}

Matching on Enums

Enums are commonly used with match expressions.

fn process(msg: Message) {
    match msg {
        Message::Quit => println!("Quit"),
        Message::Write(text) => println!("{}", text),
        Message::Move(x, y) => println!("Move to {}, {}", x, y),
        Message::ChangeColor(r, g, b) =>
            println!("Color: {}, {}, {}", r, g, b),
    }
}

The Option Enum

Rust uses the Option enum instead of null values.

enum Option {
    Some(T),
    None,
}

This makes it explicit whether a value exists or not.


Using Option

Here is an example of using Option safely.

let value = Some(10);

match value {
    Some(v) => println!("Value is {}", v),
    None => println!("No value"),
}

Why Enums Matter

Enums allow Rust programs to:

  • Model complex states
  • Avoid null pointer errors
  • Write safer control flow
  • Handle multiple data types cleanly

📝 Practice Exercises


Exercise 1

Create an enum representing different payment methods.

Exercise 2

Create an enum variant that stores data.

Exercise 3

Write a function that matches on an enum.

Exercise 4

Use Option to safely handle missing values.


✅ Practice Answers


Answer 1

enum Payment {
    Cash,
    Card,
    Online,
}

Answer 2

enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
}

Answer 3

fn describe(p: Payment) {
    match p {
        Payment::Cash => println!("Cash payment"),
        Payment::Card => println!("Card payment"),
        Payment::Online => println!("Online payment"),
    }
}

Answer 4

let num: Option = None;

What’s Next?

Now that you understand enums, the next lesson will cover pattern matching, which allows powerful control flow using enums.