Day 10: Explore Enums in Rust with examples.

Day 10: Explore Enums in Rust with examples.

Introduction:

Enums, short for enumerations, are a powerful feature in Rust that allows you to define a type that can have a fixed set of named values. Enums provide a concise way to represent and work with a finite set of related possibilities. In this article, we will explore enums in Rust and showcase examples of their usage.

Defining Enums in Rust:

Let's begin by understanding how to define and use enums in Rust.

enum Color {
    Red,
    Green,
    Blue,
}

fn main() {
    let favorite_color = Color::Blue;
    match favorite_color {
        Color::Red => println!("Red is my favorite color!"),
        Color::Green => println!("Green is my favorite color!"),
        Color::Blue => println!("Blue is my favorite color!"),
    }
}

In the example above, we define an enum called Color with three variants: Red, Green, and Blue. We then create an instance of the enum called favorite_color and use a match expression to pattern match against its value, printing a corresponding message.

Enums with Data:

Enums can also hold data associated with their variants. This allows you to attach additional information to each variant.

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

fn area(shape: &Shape) -> f64 {
    match shape {
        Shape::Circle(radius) => std::f64::consts::PI * radius * radius,
        Shape::Rectangle(width, height) => width * height,
        Shape::Square(side) => side * side,
    }
}

fn main() {
    let circle = Shape::Circle(3.0);
    let rectangle = Shape::Rectangle(2.0, 4.0);
    let square = Shape::Square(5.0);

    println!("Circle area: {:.2}", area(&circle));       // Output: Circle area: 28.27
    println!("Rectangle area: {:.2}", area(&rectangle)); // Output: Rectangle area: 8.00
    println!("Square area: {:.2}", area(&square));       // Output: Square area: 25.00
}

In this example, we define an enum Shape with three variants: Circle that holds a radius, Rectangle that holds width and height, and Square that holds the side length. The area the function uses pattern matching to calculate and return the area based on the shape variant.

Enums with Methods:

Enums in Rust can have methods associated with them, allowing you to define behavior specific to each variant.

enum State {
    Initial,
    InProgress,
    Completed,
}

impl State {
    fn is_completed(&self) -> bool {
        match self {
            State::Completed => true,
            _ => false,
        }
    }
}

fn main() {
    let initial_state = State::Initial;
    let in_progress_state = State::InProgress;
    let completed_state = State::Completed;

    println!("Is initial state completed? {}", initial_state.is_completed()); // Output: Is initial state completed? false
    println!("Is in-progress state completed? {}", in_progress_state.is_completed()); // Output: Is in-progress state completed? false
    println!("Is completed state completed? {}", completed_state.is_completed()); // Output: Is completed state completed? true
}

In this example, we define an enum State with three variants: Initial, InProgress, and Completed. We implement a method is_completed() for the State enum, which returns true only if the variant is Completed.

Conclusion:

Enums are a versatile feature in Rust that enable you to define custom types representing a fixed set of values. They can hold associated data and have methods defined on them, making them a powerful tool for expressing various possibilities in your programs.

By understanding and utilizing enums effectively, you can write more expressive and type-safe code in Rust. Experiment with different enum designs, explore pattern matching, and leverage associated data and methods to unlock the full potential of enums in your Rust projects. Happy coding in Rust!

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/@maheshwarligade

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!