5 ms·
You use composition and leverage the Deref trait to make the methods of specific fields available on the parent. This works on arbitrary levels of nesting which
by estebank 6y ago
You use composition and leverage the Deref trait to make the methods of specific fields available on the parent. This works on arbitrary levels of nesting which makes it look like OOP, but depending on what you're doing this is a good idea or not.
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=6961c82a243ed64594a7263db88110c8 https://play.rust-lang.org/?version=nightly&mode=debug&editi...
As you can see this can be verbose, but you could have derive macros (like the Debug one I'm using here) to do this boilerplate for you.
An alternative way of dealing with OOP inheritance is by using traits, where you implement the behavioral relationships by writing new traits for the behavior you want, adding super traits for them.
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f895c919b5aa26ecb2f32e1922061454 https://play.rust-lang.org/?version=nightly&mode=debug&editi...