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
Here:
enumis the keyword used to define an enum.Statusis the enum name.Active,Inactive, andGraduatedare its variants.Status::Activecreates a value of theStatusenum.
An enum value can contain only one of its variants at a time.
2. Enum Syntax
To create an enum value:
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
Output:
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.
Output:
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
Here:
Cashcontains no additional data.Cardcontains aString.Upicontains aString.
This is useful when different types of values need different information.
Accessing Enum Data Using match
Output:
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
Output:
Here:
Regularstores a name and age.Onlinestores a name and course.matchextracts the values according to the selected variant.
6. Enums with Struct-Like Variants
Enum variants can also contain named fields, similar to structs.
Example
Output:
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:
Here:
Some(T)represents an available value.Nonerepresents the absence of a value.Tis a generic type, which we will study in a later lesson.
Example
Output:
Using Option with match
Output:
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
Output:
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:
Output:
9. Enums with Methods
Just like structs, enums can have methods using an impl block.
Example
Output:
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 |
|
|
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:
Example:
Result
Used when an operation may succeed or fail.
Conceptual definition:
Example:
Here:
Ok(100)represents success.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 |
|
Create variant |
|
Match enum |
|
Wildcard pattern |
|
Enum with data |
|
Multiple values |
|
Named fields |
|
Optional value |
|
No value |
|
Conditional pattern |
|
Methods |
|