In traditional object-oriented languages (e.g. Java), it is possible to \"extend\" the functionality of a method in an inherited class by calling the original method from th
Is this a problem within Rust?
No, this is working as intended
Is there a way around it?
You can move the method to a free function and then call it directly, once from the default method and once from the "overridden" method.
fn the_default() {
println!("default implementation");
}
trait Foo {
fn method(&self) {
the_default()
}
}
struct Bar;
impl Foo for Bar {
fn method(&self) {
the_default();
println!("Hey, I'm doing something entirely different!");
}
}
fn main() {
let b = Bar;
b.method();
}
Or am I just missing something?
Rust is not an object-oriented language, Rust may be an object-oriented language, but not all OO languages are created the same. Rust may not quite fit into the traditional paradigms that you expect.
Namely, traits don't exist at run time. Only when they are applied to and used with a struct is code generated that is callable. When you create your own implementation of a method, that supersedes the default implementation; there's nowhere for the default method implementation to exist.
Often times, your code can be written in a different way. Perhaps the truly shared code should be extracted as a method on a new struct, or maybe you provide a closure to a method to customize the behavior.