Challenge - 5 Problems
Rust Generics Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Rust code using generics?
Consider this Rust function that uses generics to find the largest element in a slice. What will it print?
Rust
fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {
let mut largest = list[0];
for &item in list.iter() {
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);
}Attempts:
2 left
💡 Hint
Think about how the function compares elements and what the largest value in the list is.
✗ Incorrect
The function uses generics with trait bounds PartialOrd and Copy to compare elements. It iterates through the list and updates the largest value. The largest number in the list is 100, so it prints that.
🧠 Conceptual
intermediate1:30remaining
Why do we need generics in Rust?
Which of the following best explains why generics are needed in Rust?
Attempts:
2 left
💡 Hint
Think about code reuse and flexibility with types.
✗ Incorrect
Generics let you write code that works with many types, avoiding repetition and making code flexible and reusable.
🔧 Debug
advanced2:30remaining
What error does this Rust code produce?
This code tries to find the largest element but does not use generics properly. What error will the compiler show?
Rust
fn largest(list: &[i32]) -> i32 {
let mut largest = list[0];
for &item in list.iter() {
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);
let chars = vec!['a', 'z', 'm', 'q'];
let result = largest(&chars);
println!("The largest char is {}", result);
}Attempts:
2 left
💡 Hint
Look at the function parameter types and the arguments passed.
✗ Incorrect
The function largest only accepts slices of i32. Passing a slice of chars causes a type mismatch error at compile time.
📝 Syntax
advanced1:30remaining
Which option correctly defines a generic struct in Rust?
You want to define a struct that can hold a value of any type. Which option is correct?
Attempts:
2 left
💡 Hint
Remember Rust struct syntax and how generics are declared.
✗ Incorrect
Option D correctly declares a generic struct with fields x and y of type T. Option D misses generic declaration. Option D uses tuple struct syntax incorrectly. Option D uses semicolons inside struct fields which is invalid.
🚀 Application
expert2:00remaining
How many items are in the resulting vector after this generic function call?
Given this generic function that duplicates each element in a vector, how many elements will the result vector have?
Rust
fn duplicate_elements<T: Clone>(input: Vec<T>) -> Vec<T> {
let mut result = Vec::new();
for item in input {
result.push(item.clone());
result.push(item);
}
result
}
fn main() {
let v = vec![1, 2, 3];
let duplicated = duplicate_elements(v);
println!("Length: {}", duplicated.len());
}Attempts:
2 left
💡 Hint
Each element is added twice to the result vector.
✗ Incorrect
The function pushes each element twice, so for 3 elements, the result vector has 6 elements.