Rust Generic Quiz
40 comprehensive questions on Rust's generic programming system, covering generic functions, structs, enums, impl blocks, and monomorphization — with 11 code examples demonstrating practical generic usage patterns.
Question 1
What are generics in Rust?
Question 2
How do you define a generic function?
Question 3
What will this generic function do?
fn largest<T>(list: &[T]) -> &T {
let mut largest = &list[0];
for item in list {
if item > largest {
largest = item;
}
}
largest
}
fn main() {
let numbers = vec![34, 50, 25, 100, 65];
let result = largest(&numbers);
println!("The largest number is {}", result);
}Question 4
How do you add trait bounds to generic functions?
Question 5
What is monomorphization?
Question 6
What are the benefits of monomorphization?
Question 7
How do you define a generic struct?
Question 8
What will this generic struct instantiation create?
struct Point<T> {
x: T,
y: T,
}
fn main() {
let integer_point = Point { x: 5, y: 10 };
let float_point = Point { x: 1.0, y: 4.0 };
}Question 9
How do you implement methods for generic structs?
Question 10
What will this generic method implementation do?
struct Point<T> {
x: T,
y: T,
}
impl<T> Point<T> {
fn x(&self) -> &T {
&self.x
}
}
fn main() {
let p = Point { x: 5, y: 10 };
println!("p.x = {}", p.x());
}Question 11
How do you implement methods only for specific types in generic structs?
Question 12
What are generic enums?
Question 13
How is Option<T> defined?
Question 14
How is Result<T, E> defined?
Question 15
What is the difference between Option<T> and Result<T, E>?
Question 16
How do you use multiple generic type parameters?
Question 17
What are const generics?
Question 18
How do you define a const generic parameter?
Question 19
What is the where clause used for?
Question 20
What will this where clause example do?
fn some_function<T, U>(t: T, u: U) -> i32
where T: Display + Clone,
U: Clone + Debug
{
// function body
}Question 21
What is generic lifetime in relation to generics?
Question 22
How do you declare generic lifetimes?
Question 23
What is the relationship between generics and performance?
Question 24
What happens to binary size with generics?
Question 25
How do you create a generic type alias?
Question 26
What is generic associated type?
Question 27
How do you define a generic associated type?
Question 28
What is higher-kinded types in relation to generics?
Question 29
What is the turbofish operator (::<>)?
Question 30
When would you use the turbofish operator?
Question 31
What will this turbofish example do?
let numbers: Vec<i32> = vec![];
let result = numbers.into_iter().collect::<Vec<_>>();Question 32
What is generic covariance and contravariance?
Question 33
What is phantom data in generic contexts?
Question 34
How do you use PhantomData?
Question 35
What is generic specialization?
Question 36
What are the limitations of generics in Rust?
Question 37
How do generics interact with trait objects?
Question 38
What is generic inference?
Question 39
When does generic inference fail?
Question 40
In a scenario where you're building a generic data structure that needs to work with any comparable type, how would you design the generic constraints?
