In Rust, you can use extension traits
, that is a trait with a generic implementation for all types T
that implement the base trait:
struct Kaz {}
impl Foo for Kaz {}
trait Foo {
fn sample1(&self) -> isize { 111 }
}
trait FooExt {
fn sample2(&self);
}
impl<T: Foo> FooExt for T {
fn sample2(&self) {
println!("{}", self.sample1());
}
}
fn main() {
let x = Kaz {};
x.sample1();
x.sample2();
}