Objective
By the end of this lesson, you will understand how to define and use enums in Rust to create enumerated types. You will learn the syntax for defining enums, how to create instances, and explore various examples to demonstrate their usage.
1. What is an Enum?
In Rust, an enum (short for enumeration) is a data type that can represent a fixed set of possible values. Enums are useful for modeling scenarios where a variable can take on one of a limited number of options, making your code more expressive and type-safe.
Basic Syntax of Enums:
enum EnumName {
Variant1,
Variant2,
Variant3,
// Add more variants as needed
}
2. Defining a Basic Enum
Let’s start with a simple example: defining an enum to represent different states of a traffic light.
Example: Basic Enum
enum TrafficLight {
Red,
Yellow,
Green,
}
fn main() {
let light = TrafficLight::Red;
match light {
TrafficLight::Red => println!("Stop!"),
TrafficLight::Yellow => println!("Caution!"),
TrafficLight::Green => println!("Go!"),
}
}
In this example, we define a TrafficLight
enum with three variants: Red
, Yellow
, and Green
. We then use a match
statement to determine the action based on the current light.
3. Enums with Associated Data
Enums can also hold data, allowing you to associate different types of information with each variant. This feature adds flexibility to enums.
Example: Enum with Associated Data
enum Shape {
Circle(f64), // Variant with a radius
Rectangle(f64, f64), // Variant with width and height
}
fn area(shape: Shape) -> f64 {
match shape {
Shape::Circle(radius) => std::f64::consts::PI * radius * radius,
Shape::Rectangle(width, height) => width * height,
}
}
fn main() {
let circle = Shape::Circle(5.0);
let rectangle = Shape::Rectangle(4.0, 6.0);
println!("Area of circle: {}", area(circle)); // Outputs: Area of circle: 78.53981633974483
println!("Area of rectangle: {}", area(rectangle)); // Outputs: Area of rectangle: 24
}
In this example, the Shape
enum has two variants: Circle
, which holds a radius, and Rectangle
, which holds width and height. The area
function uses pattern matching to calculate the area based on the shape type.
4. Complex Example: Enums with Multiple Data Types
Enums can also hold different types of data in each variant, making them versatile.
Example: Enum with Multiple Data Types
enum Message {
Quit,
Move { x: i32, y: i32 }, // Struct-like variant
Write(String),
ChangeColor(i32, i32, i32), // RGB values
}
fn main() {
let messages = vec![
Message::Quit,
Message::Move { x: 10, y: 20 },
Message::Write(String::from("Hello")),
Message::ChangeColor(255, 0, 0),
];
for message in messages {
match message {
Message::Quit => println!("Quit!"),
Message::Move { x, y } => println!("Move to x: {}, y: {}", x, y),
Message::Write(text) => println!("Write message: {}", text),
Message::ChangeColor(r, g, b) => println!("Change color to RGB({}, {}, {})", r, g, b),
}
}
}
In this example, the Message
enum has multiple variants, including a struct-like variant for movement and a variant for writing messages. The match
statement processes each variant and executes the corresponding action.
5. Advanced Example: Combining Enums with Structs
You can combine enums with structs for more complex data modeling.
Example: Enum with Structs
struct User {
username: String,
email: String,
}
enum UserRole {
Admin(User),
User(User),
}
fn main() {
let admin_user = User {
username: String::from("admin"),
email: String::from("admin@example.com"),
};
let role = UserRole::Admin(admin_user);
match role {
UserRole::Admin(user) => println!("Admin: {} - {}", user.username, user.email),
UserRole::User(user) => println!("User: {} - {}", user.username, user.email),
}
}
In this example, we define a User
struct and an UserRole
enum that can represent either an Admin
or a regular User
. We then use a match
statement to handle the user role appropriately.
6. Conclusion
In this lesson, you learned how to define and use enums in Rust to create enumerated types. You explored basic enum definitions, enums with associated data, and more complex examples that demonstrate their versatility. Enums are powerful tools for creating clear and type-safe code in Rust.