rust enum examples

An enum (short for enumeration) is a way to define a type by specifying a set of possible values that it can take. Here’s a simple example of how to define an enum in Rust:

enum Color {
    Red,
    Green,
    Blue,
}

This creates a new type called Color with three possible values: Red, Green, and Blue. You can use an enum value like this:

let c: Color = Color::Red;

You can also define enum values with associated data:

enum Point {
    Cartesian(f64, f64),
    Polar(f64, f64),
}

let p: Point = Point::Cartesian(1.0, 2.0);

Here, the Point enum has two variants: Cartesian, which has two f64 values, and Polar, which also has two f64 values.

You can use a match expression to handle the different variants:

match p {
    Point::Cartesian(x, y) => println!("({}, {})", x, y),
    Point::Polar(r, theta) => println!("(r: {}, theta: {})", r, theta),
}

This will print "(1.0, 2.0)" for the value of p defined above.

An enum (short for enumeration) is a way to define a type by specifying a set of possible values that it can take. It can be thought of as a set of alternatives, where only one of the alternatives can be used at a time.

Enums are useful when you want to define a type that can take on a fixed set of values. For example, you might use an enum to represent the four suits in a deck of playing cards:

enum Suit {
    Clubs,
    Diamonds,
    Hearts,
    Spades,
}

You can then use a value of this enum type like this:

let my_suit: Suit = Suit::Hearts;

Enums can also have associated data with each variant. For example, you might want to store the rank of a playing card as well as its suit:

enum Card {
    Ace,
    Two,
    Three,
    // etc.
}

enum Suit {
    Clubs(Card),
    Diamonds(Card),
    Hearts(Card),
    Spades(Card),
}

Now each variant of the Suit enum has an associated Card value. You can use a match expression to handle the different variants and access the associated data:

let my_card = Suit::Hearts(Card::Ace);

match my_card {
    Suit::Clubs(card) => println!("{:?} of clubs", card),
    Suit::Diamonds(card) => println!("{:?} of diamonds", card),
    Suit::Hearts(card) => println!("{:?} of hearts", card),
    Suit::Spades(card) => println!("{:?} of spades", card),
}

This will print "Ace of hearts".

Here is a simple example of an enum in Rust:

enum Color {
    Red,
    Green,
    Blue,
}

let favorite_color: Color = Color::Blue;

In this example, we have defined an enum called Color with three variants: Red, Green, and Blue. We can then create a variable called favorite_color of type Color and assign it the value Color::Blue.

Enums can also have fields associated with each variant. For example:

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

let message = Message::Write("Hello, world!".to_string());

In this example, the Message enum has four variants. The Quit variant has no associated data, while the ChangeColor variant has three i32 values, the Move variant has two i32 fields, and the Write variant has a single String field.

You can use a match expression to handle each variant of an enum:

match message {
    Message::Quit => println!("The Quit variant has no data to access."),
    Message::ChangeColor(r, g, b) => println!("Change the color to red {}, green {}, and blue {}", r, g, b),
    Message::Move { x, y } => println!("Move the object to x {}, y {}", x, y),
    Message::Write(s) => println!("Write the string {}", s),
}

I hope this helps! Let me know if you have any questions.

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *