An enum with data is an enum where each variant can hold different types and amounts of data, allowing you to store values directly inside each variant.
Click to reveal answer
beginner
How do you define an enum variant that holds a single integer value?
You define it by specifying the variant name followed by parentheses containing the type, for example:
enum Example { Number(i32) }
Click to reveal answer
beginner
How can you access data stored inside an enum variant?
You use pattern matching with match or if let to extract the data from the variant.
Click to reveal answer
intermediate
What is the benefit of using enums with data over structs?
Enums with data let you represent different types of related data in one type, making your code clearer and safer when handling multiple related cases.
Click to reveal answer
beginner
Write a simple enum with two variants: one holding a string and one holding two integers.
Which syntax correctly defines an enum variant holding a floating-point number?
AVariant = f64
BVariant(f64)
CVariant: f64
DVariant<f64>
✗ Incorrect
In Rust, enum variants holding data are defined with parentheses containing the type, like Variant(f64).
How do you extract data from an enum variant in Rust?
AUsing pattern matching with match or if let
BUsing dot notation like variant.data
CUsing pointers
DUsing unsafe code
✗ Incorrect
Rust uses pattern matching with match or if let to safely extract data from enum variants.
What happens if you try to access data from the wrong enum variant without matching?
ACompile-time error
BRuntime panic
CReturns default value
DData is accessed successfully
✗ Incorrect
Rust enforces safe access at compile time, so accessing data from the wrong variant without matching causes a compile-time error.
Which of these is a valid enum with data in Rust?
Aenum Color { Red(u8) = 1, Green(u8) = 2 }
Benum Color { Red = 1, Green = 2, Blue = 3 }
Cenum Color { Red, Green(u8), Blue(u8, u8) }
Denum Color { Red: u8, Green: u8, Blue: u8 }
✗ Incorrect
Option C correctly defines variants with and without data. The others misuse syntax.
Why use enums with data instead of multiple structs?
ATo improve runtime speed only
BTo avoid using functions
CTo store data in a database
DTo group related data types under one type for easier handling
✗ Incorrect
Enums with data group related variants into one type, making code clearer and safer.
Explain how enums with data work in Rust and how you access their stored values.
Think about how you open a box to see what's inside.
You got /3 concepts.
Describe a real-life example where using an enum with data would be better than using separate structs.
Imagine different types of messages in a chat app.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of enums with data in Rust?
easy
A. To group related values that can be different types under one name
B. To create multiple copies of the same data
C. To perform arithmetic operations on numbers
D. To store only strings in a list
Solution
Step 1: Understand what enums with data do
Enums with data allow each variant to hold different types or amounts of data, grouping related but different information.
Step 2: Compare options with this understanding
Only To group related values that can be different types under one name correctly describes grouping related values of different types under one name.
Final Answer:
To group related values that can be different types under one name -> Option A
Quick Check:
Enums with data = group different types [OK]
Hint: Enums group different data types under one name [OK]
Common Mistakes:
Thinking enums only store one data type
Confusing enums with arrays or vectors
Assuming enums perform calculations
2. Which of the following is the correct syntax to define an enum variant with data in Rust?
easy
A. enum Color { Red(u8, u8, u8), Blue }
B. enum Color { Red = (u8, u8, u8), Blue }
C. enum Color { Red: (u8, u8, u8), Blue }
D. enum Color { Red -> (u8, u8, u8), Blue }
Solution
Step 1: Recall enum variant syntax with data
In Rust, enum variants with data are defined by listing the variant name followed by parentheses containing the data types.
Step 2: Check each option
enum Color { Red(u8, u8, u8), Blue } uses correct syntax: Red(u8, u8, u8). Options A, B, and C use invalid syntax with ->, =, or : which are incorrect for enum variant data.
Final Answer:
enum Color { Red(u8, u8, u8), Blue } -> Option A
Quick Check:
Variant with data uses parentheses () [OK]
Hint: Use parentheses () after variant name for data [OK]