With composition over inheritance in Rust, how does one implement shared state to go with shared behaviour?

"Composition over inheritance" does not mean "replace inheritance with composition". Composition alone is less powerful than inheritance, because inheritance is composition plus shared behavior plus some other stuff. If all you had in Rust was everything that's in Java, but no inheritance, Rust would be a less capable language.

The point of composition over inheritance (in my interpretation) is that because composition is less powerful, you can use it in more places, and combine it with other tools to make more flexible abstractions that are better suited to any given problem. The number of problems that actually call for exactly inheritance is... well, surprisingly small. So...

In general, you should not expect to take a class-based design with pervasive use of inheritance and just replace all the inheritance with something else. It may work, but it will not be as coherent as if you start from your system requirements and sketch up a new design without using inheritance. You may even find that the use of inheritance was gratuitous to begin with and the design is cleaner, less coupled and faster without it.

13 Likes