0
0
Rustprogramming~20 mins

Generic functions in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Generic Functions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a generic function with different types

What is the output of this Rust program that uses a generic function to print values?

Rust
fn print_value<T: std::fmt::Display>(value: T) {
    println!("Value: {}", value);
}

fn main() {
    print_value(42);
    print_value("hello");
}
AValue: 42\nValue: \"hello\"
BValue: 42\nValue: hello
CValue: 42\nValue: hello\nValue: 42
DCompilation error due to missing trait bound
Attempts:
2 left
💡 Hint

Look at the trait bound std::fmt::Display and how it allows printing.

🧠 Conceptual
intermediate
1:30remaining
Understanding generic function constraints

Which statement best describes why generic functions in Rust often require trait bounds?

ATrait bounds specify what operations the generic type can perform, enabling the compiler to check correctness.
BTrait bounds allow generic functions to accept any type without restrictions.
CTrait bounds are optional and only used for documentation purposes.
DTrait bounds automatically convert types to a common type inside the function.
Attempts:
2 left
💡 Hint

Think about how Rust ensures safety and correctness with generics.

🔧 Debug
advanced
2:00remaining
Identify the error in this generic function

What error does this Rust code produce?

Rust
fn add<T>(a: T, b: T) -> T {
    a + b
}

fn main() {
    let result = add(5, 10);
    println!("{}", result);
}
Aerror: mismatched types between a and b
Berror: missing lifetime specifier
Cerror: binary operation `+` cannot be applied to type `T`
DNo error, prints 15
Attempts:
2 left
💡 Hint

Consider what trait is needed to use the + operator in Rust.

📝 Syntax
advanced
1:30remaining
Correct syntax for a generic function with multiple type parameters

Which option shows the correct syntax for a generic function that takes two different types and returns a tuple of them?

Afn pair<T, U>(a: T, b: U) -> (T, U) { (a, b) }
Bfn pair<T, U>(a: T, b: U) -> (U, T) { (a, b) }
Cfn pair<T, U>(a: T, b: U) -> (T, U) { a, b }
Dfn pair<T, U>(a: T, b: U) -> (T, U) { return a, b; }
Attempts:
2 left
💡 Hint

Remember how to return a tuple in Rust.

🚀 Application
expert
2:30remaining
Using generic functions with trait bounds and lifetimes

What is the output of this Rust program?

Rust
fn longest<'a, T>(x: &'a T, y: &'a T) -> &'a T
where
    T: std::fmt::Display + PartialOrd,
{
    if x >= y {
        println!("{} is longer or equal", x);
        x
    } else {
        println!("{} is longer", y);
        y
    }
}

fn main() {
    let a = "apple";
    let b = "banana";
    let result = longest(&a, &b);
    println!("Longest: {}", result);
}
Aapple is longer or equal\nLongest: apple
BRuntime panic due to lifetime mismatch
CCompilation error due to trait bound mismatch
Dbanana is longer\nLongest: banana
Attempts:
2 left
💡 Hint

Consider how string slices compare and the trait bounds used.