Rust
December 2, 2026 5 min read

Enums in Rust

HS
HEMANT SINGH
Technical Writer & Educator

1. What is an Enum?

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

Example

enum Status {
Active,
Inactive,
Graduated,
}

fn main() {
let student_status = Status::Active;

println!("Student status created!");
}

Here:

  1. enum is the keyword used to define an enum.
  2. Status is the enum name.
  3. Active, Inactive, and Graduated are its variants.
  4. Status::Active creates a value of the Status enum.

An enum value can contain only one of its variants at a time.

2. Enum Syntax

enum EnumName {
Variant1,
Variant2,
Variant3,
}

To create an enum value:

let variable = EnumName::Variant1;

Notice that we use the :: operator to access an enum variant.

3. Using Enums with match

The match expression is commonly used to check which enum variant is present.

Example

enum Status {
Active,
Inactive,
Graduated,
}

fn main() {
let student_status = Status::Active;

match student_status {
Status::Active => println!("Student is active"),
Status::Inactive => println!("Student is inactive"),
Status::Graduated => println!("Student has graduated"),
}
}

Output:

Student is active

Here, Rust checks the enum value and executes the matching branch.

Important: Exhaustive Matching

Rust requires a match expression to handle every possible enum variant.

For example, if we define three variants, we must handle all three.

Alternatively, we can use _ as a wildcard pattern.

enum Status {
Active,
Inactive,
Graduated,
}

fn main() {
let status = Status::Inactive;

match status {
Status::Active => println!("Active"),
_ => println!("Other status"),
}
}

Output:

Other status

The _ pattern handles all variants not matched earlier.

4. Enums with Data

One of Rust's powerful features is that enum variants can contain data.

Different variants can even contain different data types.

Example

enum Payment {
Cash,
Card(String),
Upi(String),
}

fn main() {
let payment1 = Payment::Cash;
let payment2 = Payment::Card(String::from("1234"));
let payment3 = Payment::Upi(String::from("user@upi"));
}

Here:

  1. Cash contains no additional data.
  2. Card contains a String.
  3. Upi contains a String.

This is useful when different types of values need different information.

Accessing Enum Data Using match

enum Payment {
Cash,
Card(String),
Upi(String),
}

fn main() {
let payment = Payment::Upi(String::from("hemant@upi"));

match payment {
Payment::Cash => {
println!("Payment by Cash");
}

Payment::Card(number) => {
println!("Card Number: {}", number);
}

Payment::Upi(id) => {
println!("UPI ID: {}", id);
}
}
}

Output:

UPI ID: hemant@upi

The id variable receives the data stored inside the Upi variant.

5. Enum Variants with Multiple Values

An enum variant can contain multiple values.

Example

enum Student {
Regular(String, u32),
Online(String, String),
}

fn main() {
let student = Student::Regular(
String::from("Hemant"),
32,
);

match student {
Student::Regular(name, age) => {
println!("Name: {}", name);
println!("Age: {}", age);
}

Student::Online(name, course) => {
println!("Name: {}", name);
println!("Course: {}", course);
}
}
}

Output:

Name: Hemant
Age: 32

Here:

  1. Regular stores a name and age.
  2. Online stores a name and course.
  3. match extracts the values according to the selected variant.

6. Enums with Struct-Like Variants

Enum variants can also contain named fields, similar to structs.

Example

enum Employee {
Permanent {
name: String,
salary: u32,
},

Contract {
name: String,
duration: u32,
},
}

fn main() {
let employee = Employee::Permanent {
name: String::from("Rahul"),
salary: 30000,
};

match employee {
Employee::Permanent { name, salary } => {
println!("Name: {}", name);
println!("Salary: {}", salary);
}

Employee::Contract { name, duration } => {
println!("Name: {}", name);
println!("Contract Duration: {} months", duration);
}
}
}

Output:

Name: Rahul
Salary: 30000

This is useful when different variants need different named fields.

7. The Option Enum

Rust provides a built-in enum called Option.

It represents a value that may or may not exist.

Its definition is conceptually:

enum Option<T> {
Some(T),
None,
}

Here:

  1. Some(T) represents an available value.
  2. None represents the absence of a value.
  3. T is a generic type, which we will study in a later lesson.

Example

fn main() {
let age: Option<u32> = Some(32);

let unknown_age: Option<u32> = None;

println!("{:?}", age);
println!("{:?}", unknown_age);
}

Output:

Some(32)
None

Using Option with match

fn main() {
let age: Option<u32> = Some(32);

match age {
Some(value) => println!("Age: {}", value),
None => println!("Age is not available"),
}
}

Output:

Age: 32

Why Does Rust Use Option?

In some programming languages, a variable may contain null or nil.

Rust uses Option to represent the possibility that a value is missing.

This makes missing values explicit and encourages programmers to handle them safely.

8. Using if let with Enums

Sometimes we only want to check one particular enum variant.

Instead of writing a complete match, we can use if let.

Example

enum Status {
Active,
Inactive,
Graduated,
}

fn main() {
let status = Status::Active;

if let Status::Active = status {
println!("Student is active");
}
}

Output:

Student is active

if let is useful when we care about only one pattern and do not need to handle every possible variant.

We can also use else:

enum Status {
Active,
Inactive,
}

fn main() {
let status = Status::Inactive;

if let Status::Active = status {
println!("Active");
} else {
println!("Not active");
}
}

Output:

Not active

9. Enums with Methods

Just like structs, enums can have methods using an impl block.

Example

enum TrafficLight {
Red,
Yellow,
Green,
}

impl TrafficLight {
fn message(&self) {
match self {
TrafficLight::Red => println!("Stop"),
TrafficLight::Yellow => println!("Wait"),
TrafficLight::Green => println!("Go"),
}
}
}

fn main() {
let light = TrafficLight::Green;

light.message();
}

Output:

Go

Here, the message() method is associated with the TrafficLight enum.

10. Difference Between Struct and Enum


Feature

Struct

Enum

Purpose

Groups related fields

Represents one of several variants

Data

All defined fields belong to an instance

One variant is active at a time

Variants

No variants

Multiple variants

Example

Student { name, age }

Status::Active

Common use

Student or employee records

Status, payment type, user role

11. Difference Between Option and Result

Rust provides two important built-in enums:

Option

Used when a value may or may not exist.

Conceptual definition:

enum Option<T> {
Some(T),
None,
}

Example:

let age: Option<u32> = Some(32);

Result

Used when an operation may succeed or fail.

Conceptual definition:

enum Result<T, E> {
Ok(T),
Err(E),
}

Example:

let result: Result<i32, &str> = Ok(100);

Here:

  1. Ok(100) represents success.
  2. Err("Something went wrong") represents an error.

We will study Result and error handling in detail in a later lesson.

12. Quick Reference


Concept

Example

Define enum

enum Status { Active, Inactive }

Create variant

Status::Active

Match enum

match status { ... }

Wildcard pattern

_ => ...

Enum with data

Card(String)

Multiple values

Student(String, u32)

Named fields

Permanent { name: String }

Optional value

Some(100)

No value

None

Conditional pattern

if let Status::Active = status

Methods

impl Status {}