0
0
Rustprogramming~20 mins

Trait bounds in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Trait Bounds Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of function using trait bounds with multiple traits
What is the output of this Rust program?
Rust
use std::fmt::Display;

fn print_and_return<T: Display + Clone>(item: T) -> T {
    println!("Item: {}", item);
    item.clone()
}

fn main() {
    let x = print_and_return(42);
    println!("Returned: {}", x);
}
ACompilation error due to missing trait bound
BItem: 42\nReturned: 43
CItem: 42\nReturned: 42
DRuntime panic due to clone failure
Attempts:
2 left
💡 Hint
Check the trait bounds on the generic type T and what traits i32 implements.
Predict Output
intermediate
2:00remaining
Output of function with trait bound using where clause
What is the output of this Rust code snippet?
Rust
fn describe<T>(item: T) where T: std::fmt::Debug {
    println!("Debug info: {:?}", item);
}

fn main() {
    describe("hello");
}
ADebug info: hello
BDebug info: "hello"
CCompilation error: missing trait bound
DRuntime error: cannot print debug
Attempts:
2 left
💡 Hint
Look at how the Debug trait formats strings with {:?}.
Predict Output
advanced
2:00remaining
Output of generic function with trait bound and method call
What is the output of this Rust program?
Rust
trait Summary {
    fn summarize(&self) -> String;
}

struct NewsArticle {
    headline: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {
        format!("Breaking news: {}", self.headline)
    }
}

fn notify<T: Summary>(item: T) {
    println!("Notification: {}", item.summarize());
}

fn main() {
    let article = NewsArticle { headline: String::from("Rust 1.70 released") };
    notify(article);
}
ANotification: Breaking news: Rust 1.70 released
BNotification: Rust 1.70 released
CCompilation error: trait method not found
DRuntime error: method call failed
Attempts:
2 left
💡 Hint
Check how the Summary trait is implemented and used in notify.
Predict Output
advanced
2:00remaining
Output of function with multiple trait bounds and generic struct
What is the output of this Rust code?
Rust
use std::fmt::{Display, Debug};

struct Pair<T> {
    x: T,
    y: T,
}

impl<T: Display + Debug> Pair<T> {
    fn compare_and_display(&self) {
        if self.x == self.y {
            println!("Equal: {}", self.x);
        } else {
            println!("Not equal: {:?} and {:?}", self.x, self.y);
        }
    }
}

fn main() {
    let pair = Pair { x: 5, y: 5 };
    pair.compare_and_display();
}
ACompilation error: trait bound missing for PartialEq
BNot equal: 5 and 5
CEqual: 5
DRuntime error: cannot compare values
Attempts:
2 left
💡 Hint
Check which traits are required for the == operator.
🧠 Conceptual
expert
2:00remaining
Which option correctly explains trait bounds in Rust generics?
Which statement best describes the purpose of trait bounds in Rust generic functions or structs?
ATrait bounds are used to convert generic types into concrete types at compile time.
BTrait bounds automatically implement traits for all generic types used in the function or struct.
CTrait bounds restrict generic types to only primitive types like integers and floats.
DTrait bounds specify the required capabilities a generic type must have to use certain methods or operators.
Attempts:
2 left
💡 Hint
Think about why Rust needs trait bounds when calling methods on generic types.