0
0
Rustprogramming~20 mins

Generics with traits in Rust - Practice Problems & Coding Challenges

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

What is the output of this Rust program?

Rust
use std::fmt::Display;

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

fn main() {
    print_value(42);
    print_value("hello");
}
AValue: 42\nValue: hello
BValue: 42\nValue: \"hello\"
CCompilation error: trait bound not satisfied
DValue: 42\nValue: hello\nValue: 42
Attempts:
2 left
💡 Hint

Look at the trait bound Display and how it formats values.

🧠 Conceptual
intermediate
2:00remaining
Trait bounds in generic structs

Which statement about the following Rust code is true?

struct Container {
    value: T,
}

impl Container {
    fn duplicate(&self) -> (T, T) {
        (self.value.clone(), self.value.clone())
    }
}
AThe duplicate() method will fail to compile for all types.
BThe struct can hold any type, but duplicate() requires Clone.
CThe struct requires Copy trait, not Clone.
DThe struct can only hold types that implement the Clone trait.
Attempts:
2 left
💡 Hint

Look at the trait bound on the struct definition.

🔧 Debug
advanced
2:00remaining
Fix the trait bound error

What error does this Rust code produce?

fn compare(a: T, b: T) -> bool {
    a == b
}

fn main() {
    println!("{}", compare(5, 5));
}
Aerror: function main must return ()
Berror: missing semicolon after println!
Cerror: binary operation `==` cannot be applied to type `T`
DNo error, prints true
Attempts:
2 left
💡 Hint

Think about what traits are needed to use == operator.

Predict Output
advanced
2:00remaining
Output of generic trait implementation

What is the output of this Rust program?

Rust
trait Summary {
    fn summarize(&self) -> String;
}

struct NewsArticle {
    headline: String,
    location: String,
}

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

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

fn main() {
    let article = NewsArticle {
        headline: String::from("Rust 1.60 Released"),
        location: String::from("Internet"),
    };
    notify(article);
}
ABreaking news: Rust 1.60 Released, Internet
BBreaking news: Rust 1.60 Released - Internet
CCompilation error: missing trait implementation
DBreaking news: NewsArticle
Attempts:
2 left
💡 Hint

Look at the summarize method implementation and the notify function.

🧠 Conceptual
expert
3:00remaining
Trait object vs generic parameter

Which statement correctly explains the difference between using a trait object &dyn Trait and a generic parameter T: Trait in Rust?

A<p><code>&dyn Trait</code> uses dynamic dispatch and allows different types at runtime; <code>T: Trait</code> uses static dispatch and requires a single type at compile time.</p>
B<p><code>&dyn Trait</code> requires the type to implement <code>Copy</code>; <code>T: Trait</code> does not.</p>
C<p><code>T: Trait</code> uses dynamic dispatch; <code>&dyn Trait</code> uses static dispatch.</p>
D<p>Both are identical in performance and usage.</p>
Attempts:
2 left
💡 Hint

Think about when Rust decides which method to call: compile time or runtime.