Challenge - 5 Problems
Rust Trait Bounds Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
}Attempts:
2 left
💡 Hint
Check the trait bounds on the generic type T and what traits i32 implements.
✗ Incorrect
The function requires T to implement Display and Clone. The integer 42 implements both traits, so the function prints the item and returns a clone of it. The output shows the printed item and the returned value.
❓ Predict Output
intermediate2: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");
}Attempts:
2 left
💡 Hint
Look at how the Debug trait formats strings with {:?}.
✗ Incorrect
The Debug trait formats string slices with quotes, so the output includes quotes around hello.
❓ Predict Output
advanced2: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);
}Attempts:
2 left
💡 Hint
Check how the Summary trait is implemented and used in notify.
✗ Incorrect
The notify function requires T to implement Summary and calls summarize. The NewsArticle struct implements Summary, so the output is the formatted string from summarize.
❓ Predict Output
advanced2: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();
}Attempts:
2 left
💡 Hint
Check which traits are required for the == operator.
✗ Incorrect
The == operator requires the PartialEq trait, but Pair's impl only requires Display and Debug, so the code fails to compile due to missing PartialEq bound.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about why Rust needs trait bounds when calling methods on generic types.
✗ Incorrect
Trait bounds tell the compiler what traits a generic type must implement so that the code can safely call methods or use operators defined by those traits.