Recall & Review
beginner
What is a generic enum in Rust?
A generic enum in Rust is an enum that can hold different types specified by a generic parameter. It allows the enum to be flexible and work with various data types.
Click to reveal answer
beginner
How do you define a generic enum with one type parameter in Rust?
You add a type parameter inside angle brackets after the enum name, like <T>. For example: <br>
enum Option<T> {
Some(T),
None,
}Click to reveal answer
intermediate
Why use generic enums instead of fixed-type enums?
Generic enums let you write one enum that works with many types, reducing code duplication and increasing flexibility. For example, Option<T> can hold any type T, not just one fixed type.Click to reveal answer
intermediate
Can generic enums have multiple type parameters? Give an example.
Yes, generic enums can have multiple type parameters. Example:<br>
enum Result {
Ok(T),
Err(E),
} This enum can hold a success value of type T or an error value of type E.Click to reveal answer
beginner
How do you create an instance of a generic enum with a specific type?
You specify the type when you use the enum. For example:<br><pre>let x: Option<i32> = Option::Some(5);</pre> Here, Option is used with i32 as the type parameter.Click to reveal answer
What does the <T> mean in
enum Option<T>?✗ Incorrect
The declares a generic type parameter, allowing the enum to hold any type specified when used.
Which of these is a valid generic enum definition?
✗ Incorrect
Option B correctly defines a generic enum with two type parameters and variants holding those types.
How do you create an Option holding a string "hello"?
✗ Incorrect
Option::Some("hello") creates an Option holding a string slice. The type is inferred or can be specified explicitly.
What is the benefit of using generic enums?
✗ Incorrect
Generic enums increase flexibility by allowing the same enum to hold different types.
Can you use a generic enum without specifying the type parameter?
✗ Incorrect
You must specify the type parameter or let Rust infer it from context.
Explain what a generic enum is and why it is useful in Rust.
Think about how enums can hold different types using generics.
You got /3 concepts.
Write a simple generic enum with two variants and explain how to create an instance of it.
Use something like Result<T, E> or Option<T>.
You got /3 concepts.