📖 Scenario: You are creating a simple program to represent different types of fruits using Rust enums. This helps organize related values under one type.
🎯 Goal: Build a Rust program that defines an enum called Fruit with specific fruit variants, then use it in a variable and print a message based on the fruit.
📋 What You'll Learn
Define an enum named Fruit with variants Apple, Banana, and Cherry
Create a variable named my_fruit and assign it the Fruit::Banana variant
Use a match expression on my_fruit to print a message for each fruit variant
💡 Why This Matters
🌍 Real World
Enums are used in many programs to represent fixed sets of options, like states, commands, or categories.
💼 Career
Understanding enums is important for Rust programming jobs, especially in system programming, game development, and backend services.
Progress0 / 4 steps
1
Define the enum Fruit
Write a Rust enum named Fruit with variants Apple, Banana, and Cherry.
Rust
Hint
An enum groups related values. Use enum Fruit { Apple, Banana, Cherry }.
2
Create a variable with a Fruit variant
Create a variable named my_fruit and assign it the Fruit::Banana variant.
Rust
Hint
Use let my_fruit = Fruit::Banana; to create the variable.
3
Use match to handle the Fruit variants
Write a match expression on my_fruit that prints "It's an apple!" for Fruit::Apple, "It's a banana!" for Fruit::Banana, and "It's a cherry!" for Fruit::Cherry.
Rust
Hint
Use match my_fruit { Fruit::Apple => println!("It's an apple!"), ... }.
4
Print the message for the selected fruit
Run the program so it prints the message for my_fruit. Use println! inside the match as before.
Rust
Hint
Wrap your code in fn main() { ... } and run it to see the message.
Practice
(1/5)
1. What is the main purpose of defining an enum in Rust?
easy
A. To create a function that returns multiple values
B. To write a loop that iterates over numbers
C. To define a variable that can only hold integers
D. To group related values under one type name
Solution
Step 1: Understand what enums represent
Enums group related values under a single type name, making code clearer and safer.
Step 2: Compare options with enum purpose
Only To group related values under one type name correctly describes grouping related values under one type name.
Final Answer:
To group related values under one type name -> Option D
Quick Check:
Enums = Group related values [OK]
Hint: Enums group related values under one name [OK]
Common Mistakes:
Thinking enums are for loops or functions
Confusing enums with variables or constants
Believing enums only hold integers
2. Which of the following is the correct syntax to define a simple enum named Color with variants Red, Green, and Blue?
easy
A. enum Color { Red, Green, Blue }
B. enum Color = { Red, Green, Blue }
C. enum Color: Red, Green, Blue;
D. enum Color { Red; Green; Blue; }
Solution
Step 1: Recall Rust enum syntax
Rust enums use curly braces with comma-separated variants, no equals or semicolons.
Step 2: Check each option
enum Color { Red, Green, Blue } matches correct syntax: enum Color { Red, Green, Blue }.
Final Answer:
enum Color { Red, Green, Blue } -> Option A
Quick Check:
Enum syntax = curly braces with commas [OK]
Hint: Use curly braces and commas for enum variants [OK]
Common Mistakes:
Using '=' instead of braces
Separating variants with semicolons
Using colon ':' after enum name
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 South
B. Going North
C. Going East
D. Going West
Solution
Step 1: Identify the 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 enum variant to print correct message [OK]
Common Mistakes:
Confusing variant names in match arms
Forgetting to use enum name with variants
Assuming default output without match
4. Identify the error in this enum definition:
enum Status {
Active,
Inactive,
Pending()
}
medium
A. Enum variants must all have parentheses
B. The variant 'Pending' should not have parentheses unless it holds data
C. Enum name cannot be 'Status'
D. Missing semicolon after enum definition
Solution
Step 1: Check variant syntax
Variants without data should not have parentheses; 'Pending()' is incorrect here.
Step 2: Confirm correct enum variant style
Only variants holding data use parentheses; others are simple names.
Final Answer:
The variant 'Pending' should not have parentheses unless it holds data -> Option B
Quick Check:
Simple variants = no parentheses [OK]
Hint: Only use parentheses for variants holding data [OK]