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?
AThe default method implementation is used.
BCompilation fails with an error.
CThe method is ignored and not available.
DThe program crashes at runtime.
✗ 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?
Afn
Bstruct
Cimpl
Dtrait
✗ 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?
ANo, it can only use its own code.
BYes, including methods without default implementations.
COnly if those methods are also default methods.
DOnly if the trait is marked as 'default'.
✗ 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?
ANothing, it can skip implementing it.
BMark the method as optional.
CProvide its own implementation of that method.
DUse the default method from another trait.
✗ 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?
AThey allow code reuse and easier trait evolution.
BThey force all types to implement every method.
CThey prevent overriding methods.
DThey make traits slower to compile.
✗ 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.
Practice
(1/5)
1. What is the main purpose of default method implementations in Rust traits?
easy
A. To automatically generate structs from traits
B. To force all types to implement the method themselves
C. To prevent any method from being used outside the trait
D. To provide reusable behavior that types can use or override
Solution
Step 1: Understand trait default methods
Default methods in traits provide a common implementation that types can use without writing their own code.
Step 2: Compare with other options
Options A, B, and C describe behaviors that are not true for default methods in Rust traits.
Final Answer:
To provide reusable behavior that types can use or override -> Option D
Quick Check:
Default methods = reusable behavior [OK]
Hint: Default methods give ready code to reuse or replace [OK]
Common Mistakes:
Thinking default methods force implementation
Confusing traits with structs
Believing default methods hide methods
2. Which of the following is the correct syntax to define a default method in a Rust trait?
easy
A. fn greet() { println!("Hello"); }
B. fn greet();
C. fn greet() -> { println!("Hello"); }
D. fn greet() => println!("Hello");
Solution
Step 1: Recall Rust trait method syntax
Default methods in traits have a body with curly braces, like fn greet() { ... }.
Step 2: Check each option
fn greet() { println!("Hello"); } uses correct syntax with braces. fn greet(); is a method signature without body (no default). Options A and D have invalid syntax.
Final Answer:
fn greet() { println!("Hello"); } -> Option A
Quick Check:
Default method = fn name() { body } [OK]
Hint: Default methods always have braces with code inside [OK]
Common Mistakes:
Omitting braces for default methods
Using arrow syntax instead of braces
Writing only method signatures without body
3. What will be the output of this Rust code?
trait Speak {
fn say(&self) {
println!("Default speaking");
}
}
struct Dog;
impl Speak for Dog {}
fn main() {
let d = Dog;
d.say();
}
medium
A. Dog says woof
B. Compilation error: method say not implemented
C. Default speaking
D. No output
Solution
Step 1: Analyze trait and struct implementation
The trait Speak has a default method say that prints "Default speaking". Dog implements Speak but does not override say.
Step 2: Determine method called
Since Dog does not override say, calling d.say() uses the default method from the trait.
Final Answer:
Default speaking -> Option C
Quick Check:
Default method runs if not overridden [OK]
Hint: If no override, default method runs [OK]
Common Mistakes:
Expecting an error for missing method
Assuming default method is not used
Confusing struct behavior with trait methods
4. Identify the error in this Rust code using default method implementations:
trait Talk {
fn talk(&self) {
println!("Talking");
}
}
struct Cat;
impl Talk for Cat {
fn talk(&self);
}
fn main() {
let c = Cat;
c.talk();
}
medium
A. Trait Talk must not have default methods
B. Missing method body in impl causes compilation error
C. Struct Cat cannot implement Talk
D. No error, code runs fine
Solution
Step 1: Check impl block for talk method
The impl for Cat declares fn talk(&self); without a body, which is invalid syntax in Rust.
Step 2: Understand default method usage
Since Talk trait has a default talk method, Cat can omit implementing it or provide a full method body. Declaring without body causes error.
Final Answer:
Missing method body in impl causes compilation error -> Option B
Quick Check:
Impl methods need bodies unless default used [OK]
Hint: Impl methods must have bodies or be omitted [OK]
Common Mistakes:
Declaring method without body in impl
Thinking default method forces impl
Ignoring compiler error messages
5. Given this trait and structs, how can you override the default method only for Cat while Dog uses the default?
trait Animal {
fn sound(&self) {
println!("Some sound");
}
}
struct Dog;
struct Cat;
impl Animal for Dog {}
impl Animal for Cat {
// What goes here?
}
fn main() {
let dog = Dog;
dog.sound();
let cat = Cat;
cat.sound();
}
hard
A. fn sound(&self) { println!("Meow"); }
B. fn sound(&self);
C. fn sound() { println!("Meow"); }
D. fn sound(&self) -> String { "Meow".to_string() }
Solution
Step 1: Understand overriding default methods
To override a default method, provide a method with the same signature and a body in the impl block.