Rust Traits Quiz
40 comprehensive questions on Rust's trait system, covering trait definitions, implementations, bounds, associated types, and trait objects — with 11 code examples demonstrating practical trait usage patterns.
Question 1
What is a trait in Rust?
Question 2
How do you define a trait in Rust?
Question 3
How do you implement a trait for a type?
Question 4
What will this trait implementation do?
trait Drawable {
fn draw(&self);
}
struct Circle;
impl Drawable for Circle {
fn draw(&self) {
println!("Drawing a circle");
}
}
fn main() {
let c = Circle;
c.draw();
}Question 5
What are trait bounds in generic functions?
Question 6
How do you specify trait bounds in function signatures?
Question 7
What will this generic function with trait bounds do?
trait Summary {
fn summarize(&self) -> String;
}
fn notify<T: Summary>(item: &T) {
println!("Breaking news! {}", item.summarize());
}
struct NewsArticle {
headline: String,
}
impl Summary for NewsArticle {
fn summarize(&self) -> String {
format!("Headline: {}", self.headline)
}
}
fn main() {
let article = NewsArticle { headline: String::from("Rust is great!") };
notify(&article);
}Question 8
What are associated types in traits?
Question 9
How do you define an associated type in a trait?
Question 10
How do you specify associated types in trait implementations?
Question 11
What is the difference between generics and associated types?
Question 12
What are trait objects?
Question 13
How do you create a trait object?
Question 14
What is the difference between static and dynamic dispatch?
Question 15
What will this trait object code do?
trait Animal {
fn make_sound(&self);
}
struct Dog;
struct Cat;
impl Animal for Dog {
fn make_sound(&self) { println!("Woof!"); }
}
impl Animal for Cat {
fn make_sound(&self) { println!("Meow!"); }
}
fn main() {
let animals: Vec<Box<dyn Animal>> = vec![Box::new(Dog), Box::new(Cat)];
for animal in animals {
animal.make_sound();
}
}Question 16
What are default implementations in traits?
Question 17
How do you provide a default implementation in a trait?
Question 18
Can you override default implementations?
Question 19
What are supertraits?
Question 20
How do you specify supertraits?
Question 21
What are orphan rules in trait implementations?
Question 22
When can you implement a trait for a type?
Question 23
What are marker traits?
Question 24
What is the Copy trait?
Question 25
What is the difference between Copy and Clone?
Question 26
How do you derive common traits automatically?
Question 27
What traits can typically be derived?
Question 28
What is trait coherence (also called orphan rules)?
Question 29
What are blanket implementations?
Question 30
What will this blanket implementation do?
trait Display {
fn display(&self) -> String;
}
impl<T: std::fmt::Display> Display for T {
fn display(&self) -> String {
format!("{}", self)
}
}
fn main() {
let num = 42;
println!("{}", num.display());
}Question 31
What are associated constants in traits?
Question 32
How do you define an associated constant?
Question 33
What is the Sized trait?
Question 34
What does ?Sized mean in trait bounds?
Question 35
What are object safety rules for traits?
Question 36
What makes a trait object-safe?
Question 37
What is the Drop trait used for?
Question 38
How do you implement the Drop trait?
Question 39
What is the Deref trait used for?
Question 40
In a scenario where you're designing a drawing API that needs to work with different shapes that can be rendered to different backends, how would you structure the trait hierarchy?
