Recall & Review
beginner
What is a default method implementation in Rust traits?
A default method implementation in Rust traits is a method defined inside a trait with a body. Types implementing the trait can use this default method without needing to provide their own version.
Click to reveal answer
beginner
How do you override a default method implementation in Rust?
To override a default method, the implementing type provides its own method with the same name and signature inside the impl block for the trait.
Click to reveal answer
intermediate
Why are default method implementations useful in Rust?
They allow trait authors to provide common behavior that many types can share, reducing code duplication and making it easier to add new methods without breaking existing implementations.
Click to reveal answer
beginner
Can a trait have methods without default implementations?
Yes. Traits can have methods without default implementations, which must be implemented by any type that implements the trait.
Click to reveal answer
beginner
Show a simple Rust trait with a default method implementation.
```rust
trait Greet {
fn say_hello(&self) {
println!("Hello from default method!");
}
}
struct Person;
impl Greet for Person {}
fn main() {
let p = Person;
p.say_hello(); // Prints: Hello from default method!
}
```Click to reveal answer
What happens if a type implementing a trait does NOT override a default method?
✗ Incorrect
If a type does not override a default method, Rust uses the default implementation provided in the trait.
Which keyword is used to define a trait in Rust?
✗ Incorrect
The keyword 'trait' is used to define traits in Rust.
Can a default method in a trait call other methods of the same trait?
✗ Incorrect
Default methods can call other trait methods, even those without default implementations, allowing flexible behavior.
What must a type do if a trait method has no default implementation?
✗ Incorrect
If a trait method has no default implementation, the implementing type must provide its own version.
Which of these is a benefit of default method implementations?
✗ Incorrect
Default methods help reuse code and let traits evolve without breaking existing implementations.
Explain what default method implementations are in Rust traits and why they are useful.
Think about how traits can provide behavior without forcing every type to write the same code.
You got /3 concepts.
Describe how you would override a default method implementation in a Rust trait for a specific type.
Consider how you customize behavior for your own types.
You got /3 concepts.