📖 Scenario: Imagine you are building a simple program to represent different types of messages in a chat app. Each message can be a text, an image, or a video.
🎯 Goal: You will create an enum called Message with variants for Text, Image, and Video. Then you will write code to create a message and print its type.
📋 What You'll Learn
Create an enum called Message with variants Text, Image, and Video
Create a variable called msg and assign it the Text variant with the value "Hello!"
Use a match expression on msg to print the type of message
Print the message type exactly as "This is a text message.", "This is an image message.", or "This is a video message."
💡 Why This Matters
🌍 Real World
Enums are used to represent different types of data that belong to one category, like different message types in chat apps.
💼 Career
Understanding enums and pattern matching is essential for Rust programming jobs, especially in system programming, web backend, and game development.
Progress0 / 4 steps
1
Create the enum Message with variants
Create an enum called Message with three variants: Text, Image, and Video. Each variant should hold a String value.
Rust
Hint
Use enum Message { Text(String), Image(String), Video(String) } to define the enum.
2
Create a variable msg with the Text variant
Create a variable called msg and assign it the Text variant of Message with the value "Hello!".
Rust
Hint
Use let msg = Message::Text(String::from("Hello!")); to create the variable.
3
Use match to check the variant of msg
Use a match expression on msg with variable m to match each variant: Text, Image, and Video. For each case, print the message type as a string (do not print the inner string).
Rust
Hint
Use match msg { Message::Text(m) => ..., Message::Image(m) => ..., Message::Video(m) => ... }.
4
Print the message type
Run the program to print the message type for the msg variable. The output should be exactly This is a text message.
Rust
Hint
Make sure you run the program inside a fn main() function.
Practice
(1/5)
1. What is an enum variant in Rust?
easy
A. A function inside a struct
B. A named option inside an enum representing a possible value
C. A variable declared with let
D. A type of loop in Rust
Solution
Step 1: Understand enum structure
An enum groups related named options called variants.
Step 2: Define variant role
Each variant represents a possible value the enum can hold.
Final Answer:
A named option inside an enum representing a possible value -> Option B
Quick Check:
Enum variant = named option [OK]
Hint: Enum variants are named choices inside enums [OK]
Common Mistakes:
Confusing variants with functions
Thinking variants are variables
Mixing variants with loops
2. Which of the following is the correct syntax to define an enum variant named Red inside an enum Color?
easy
A. enum Color { Red }
B. enum Color = Red;
C. struct Color { Red }
D. let Color = Red;
Solution
Step 1: Recall enum syntax
Enums use the syntax enum Name { Variant1, Variant2 }.
Step 2: Match correct syntax
enum Color { Red } correctly defines enum Color with variant Red.
Final Answer:
enum Color { Red } -> Option A
Quick Check:
Enum variant syntax = enum Name { Variant } [OK]
Hint: Enum variants go inside braces after enum name [OK]
Common Mistakes:
Using '=' instead of braces
Defining variants inside struct
Using let instead of enum
3. What will be the output of this Rust code?
enum Direction {
North,
East,
South,
West,
}
fn main() {
let dir = Direction::East;
match dir {
Direction::North => println!("Going North"),
Direction::East => println!("Going East"),
Direction::South => println!("Going South"),
Direction::West => println!("Going West"),
}
}
medium
A. Going North
B. Going South
C. Going East
D. Going West
Solution
Step 1: Identify enum variant assigned
The variable dir is set to Direction::East.
Step 2: Match statement output
The match arm for Direction::East prints "Going East".
Final Answer:
Going East -> Option C
Quick Check:
Match on Direction::East prints "Going East" [OK]
Hint: Match prints message for assigned enum variant [OK]
Common Mistakes:
Confusing variant names
Ignoring match arms
Assuming default output
4. Identify the error in this enum variant usage:
enum Status {
Active,
Inactive,
}
fn main() {
let s = Status::active;
}
medium
A. Enum variants must have data inside parentheses
B. Missing semicolon after enum definition
C. Cannot assign enum variant to variable
D. Enum variant names are case-sensitive; should be 'Active'
Solution
Step 1: Check variant usage
The code uses Status::active but enum defines Active with uppercase A.
Step 2: Understand Rust case sensitivity
Rust enum variants are case-sensitive; active is undefined.
Final Answer:
Enum variant names are case-sensitive; should be 'Active' -> Option D