This allows variants to carry different types and amounts of data.
Click to reveal answer
beginner
How do you create an instance of an enum variant with data?
You call the variant like a function with the required data. For example:<br><pre>let msg = Message::Move { x: 10, y: 20 };</pre>creates a <code>Move</code> variant with x and y values.
Click to reveal answer
beginner
What is the benefit of using enum variants in Rust?
Enum variants let you group related but different types of data under one type. This helps write clear, safe code that handles different cases explicitly.
Click to reveal answer
Which keyword is used to define an enum in Rust?
Aenum
Bstruct
Cvariant
Dtype
✗ Incorrect
The enum keyword defines an enum type in Rust.
How do you access the East variant of an enum named Direction?
ADirection[East]
BDirection.East
CDirection->East
DDirection::East
✗ Incorrect
Rust uses double colons :: to access enum variants, like Direction::East.
Which of these enum variants holds data?
ANorth
BQuit
CMove { x: i32, y: i32 }
DWest
✗ Incorrect
The Move variant holds data fields x and y.
What is the correct way to create a Write variant holding a string "hello"?
AMessage.Write("hello")
BMessage::Write("hello")
CMessage::Write { "hello" }
DMessage.Write { "hello" }
✗ Incorrect
You create data-holding variants by calling them like functions: Message::Write("hello").
Why use enum variants instead of separate structs?
ATo group related types under one name
BTo avoid using functions
CTo make code slower
DTo prevent any data from being stored
✗ Incorrect
Enum variants group related but different data types under one type, making code clearer and safer.
Explain what enum variants are and how they can hold data in Rust.
Think about how an enum can represent different states or messages.
You got /3 concepts.
Describe how to create and use an enum variant instance with data in Rust.
Remember how you call a variant to create a value.
You got /3 concepts.
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