Default Methods
Traits can implement behavior in terms of other trait methods:
trait Equals {
fn equal(&self, other: &Self) -> bool;
fn not_equal(&self, other: &Self) -> bool {
!self.equal(other)
}
}
#[derive(Debug)]
struct Centimeter(i16);
impl Equals for Centimeter {
fn equal(&self, other: &Centimeter) -> bool {
self.0 == other.0
}
}
fn main() {
let a = Centimeter(10);
let b = Centimeter(20);
println!("{a:?} equals {b:?}: {}", a.equal(&b));
println!("{a:?} not_equals {b:?}: {}", a.not_equal(&b));
}