An enum (short for enumeration) in Rust is a type that can be one of several different variants. It lets you define a type by listing possible values it can have.
Click to reveal answer
beginner
Why do we use enums instead of multiple variables?
Enums group related values under one type, making code easier to read and safer by preventing invalid values.
Click to reveal answer
intermediate
How do enums help with handling different states in a program?
Enums let you represent different states clearly, so you can write code that matches each state and handles it properly.
Click to reveal answer
intermediate
What advantage do enums provide when working with pattern matching?
Enums work well with Rust's pattern matching, allowing concise and clear handling of each variant without errors.
Click to reveal answer
beginner
Give a real-life example where enums are useful.
Enums are useful for things like traffic lights, where the light can be Red, Yellow, or Green. This keeps the states organized and clear.
Click to reveal answer
What does an enum represent in Rust?
AA function that returns multiple values
BA type with multiple possible variants
CA variable that changes type
DA loop that runs multiple times
✗ Incorrect
An enum defines a type that can be one of several variants.
Why are enums safer than using separate variables for states?
AThey allow variables to change type dynamically
BThey run faster than variables
CThey use less memory than variables
DThey prevent invalid values by restricting possible variants
✗ Incorrect
Enums restrict values to defined variants, preventing invalid states.
Which Rust feature works best with enums to handle different cases?
ALoops
BFunctions
CPattern matching
DMacros
✗ Incorrect
Pattern matching lets you handle each enum variant clearly and safely.
What is a common use case for enums?
ARepresenting different states or options
BStoring large amounts of data
CPerforming mathematical calculations
DCreating user interfaces
✗ Incorrect
Enums are used to represent different states or options clearly.
Which of these is NOT a benefit of using enums?
AAutomatic memory management
BSafer code by limiting values
CImproved code readability
DClear state representation
✗ Incorrect
Automatic memory management is handled by Rust's ownership system, not enums.
Explain why enums are useful in Rust programs.
Think about how enums help organize and control possible values.
You got /4 concepts.
Describe a real-life example where using an enum would be helpful.
Consider situations with a fixed set of choices.
You got /3 concepts.
Practice
(1/5)
1. Why are enums used in Rust programming?
easy
A. To group related values under one type name
B. To perform mathematical calculations faster
C. To store large amounts of unrelated data
D. To replace all functions with a single keyword
Solution
Step 1: Understand the purpose of enums
Enums allow grouping of related options or variants under one type name, making code organized.
Step 2: Compare other options
Options A, B, and D do not describe the main use of enums. Enums are not for math speed, unrelated data storage, or replacing functions.
Final Answer:
To group related values under one type name -> Option A
Quick Check:
Enums group related options = C [OK]
Hint: Enums group related choices under one name [OK]
Common Mistakes:
Thinking enums speed up math operations
Confusing enums with arrays or structs
Believing enums replace functions
2. Which of the following is the correct way to define an enum in Rust?
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 are defined using the keyword enum followed by the name and curly braces containing variants separated by commas.
Step 2: Check each option
enum Color { Red, Green, Blue } matches correct syntax. Options A, B, and C use invalid symbols (=, :, ->) which are not part of enum syntax.
Final Answer:
enum Color { Red, Green, Blue } -> Option A
Quick Check:
Correct enum syntax uses braces and commas = D [OK]
Hint: Remember enum uses braces and commas, no equal sign [OK]
Common Mistakes:
Using '=' instead of braces
Using ':' or '->' after enum name
Omitting commas between variants
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 East
C. Going South
D. Going West
Solution
Step 1: Identify the enum variant assigned
The variable dir is assigned Direction::East.
Step 2: Match statement output
The match checks dir and prints the string for the matching variant. Since dir is East, it prints "Going East".
Final Answer:
Going East -> Option B
Quick Check:
Match prints variant name = A [OK]
Hint: Match prints message for assigned enum variant [OK]
Common Mistakes:
Confusing variant names in match arms
Assuming default output without match
Ignoring enum variant assigned
4. Find the error in this Rust enum usage:
enum Status {
Active,
Inactive,
}
fn main() {
let s = Status::active;
match s {
Status::Active => println!("Active"),
Status::Inactive => println!("Inactive"),
}
}
medium
A. Match arms should use lowercase variants
B. Missing semicolon after enum definition
C. Enum variants must be capitalized exactly
D. Enum cannot be used inside match statements
Solution
Step 1: Check enum variant usage
Rust enum variants are case sensitive and usually start with uppercase letters. The code uses Status::active instead of Status::Active.
Step 2: Verify match arms and syntax
Match arms use correct capitalization. No semicolon is needed after enum. Enums can be matched.
Final Answer:
Enum variants must be capitalized exactly -> Option C
Quick Check:
Enum variant names are case sensitive = A [OK]
Hint: Enum variants are case sensitive and capitalized [OK]
Common Mistakes:
Using lowercase enum variants
Adding unnecessary semicolons after enum
Thinking enums can't be matched
5. You want to represent a shape that can be either a Circle with a radius or a Rectangle with width and height. Why is using an enum the best choice in Rust?
hard
A. Because enums automatically calculate area for shapes
B. Because enums are faster than structs for storing data
C. Because enums store all shapes as the same data type without variants
D. Because enums can hold different data types for each variant safely
Solution
Step 1: Understand enum variant data
Rust enums can hold different data types for each variant, like a radius for Circle and width/height for Rectangle.
Step 2: Compare other options
Enums do not automatically calculate area (A), nor store all shapes as same data without variants (C). Speed (D) is not the main reason.
Final Answer:
Because enums can hold different data types for each variant safely -> Option D
Quick Check:
Enums hold variant-specific data = B [OK]
Hint: Enums hold different data per variant safely [OK]
Common Mistakes:
Thinking enums calculate values automatically
Confusing enums with structs storing same data
Assuming enums are always faster than structs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.