Get the latest tech news
Behavior Inheritance in Rust (2021)
There is a common adage in Rust community that “Rust prefers composition over inheritance”. There is no inheritance support in common OO sense in Rust. You can achieve some OOP paradigm…
Lack of behavior inheritance became obvious, when I started to think about building a GUI toolkit in Rust. I raised the question on Bevy’s Discord and after a lengthy discussion we worked-out a satisfactory solution.The pattern is similar to P-Impl – have an object which provides default implementation (fields and methods) and compose it into your own object. But instead of manually defining all proxy-methods, have a trait that provides them – this trait includes a method to get the base implementation object, also implementing the same trait.The only thing you need to do in your own object to inherit on another, is to add a field with base object and trait implementation with one function returning reference to that field.
Or read this on Hacker News