Use default method implementations in traits by defining the method body directly inside the trait block, allowing implementors to skip the method if they want the default behavior.
trait Foo {
fn default_method(&self) {
println!("Default implementation");
}
fn required_method(&self);
}
struct MyStruct;
impl Foo for MyStruct {
fn required_method(&self) {
println!("Required implementation");
}
}
fn main() {
let s = MyStruct;
s.default_method(); // Uses the trait's default
s.required_method(); // Uses the struct's implementation
}