0
0
Rustprogramming~20 mins

Why generics are needed in Rust - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Generics Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
}
AThe largest number is 100
BRuntime panic due to empty slice
CCompilation error due to missing trait bounds
DThe largest number is 65
Attempts:
2 left
💡 Hint
Think about how the function compares elements and what the largest value in the list is.
🧠 Conceptual
intermediate
1:30remaining
Why do we need generics in Rust?
Which of the following best explains why generics are needed in Rust?
ATo allow functions and types to work with multiple data types without duplicating code
BTo enforce that all variables must be of the same type throughout the program
CTo make the compiler ignore type checking for faster compilation
DTo restrict functions to only work with primitive types
Attempts:
2 left
💡 Hint
Think about code reuse and flexibility with types.
🔧 Debug
advanced
2: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);
}
ARuntime error: index out of bounds
BCompilation error: mismatched types, expected i32 but found char
CCompilation error: missing lifetime specifier
DThe program runs and prints the largest number and char
Attempts:
2 left
💡 Hint
Look at the function parameter types and the arguments passed.
📝 Syntax
advanced
1: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?
Astruct Point<T> { x: T; y: T; }
Bstruct Point { x: T, y: T }
Cstruct Point<T> (x: T, y: T);
Dstruct Point<T> { x: T, y: T }
Attempts:
2 left
💡 Hint
Remember Rust struct syntax and how generics are declared.
🚀 Application
expert
2: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());
}
A9
B3
C6
D4
Attempts:
2 left
💡 Hint
Each element is added twice to the result vector.