2021-05-14
2108
#rust
Andre Bogus
48759
May 14, 2021 â‹… 7 min read

Understanding inheritance and other limitations in Rust

Andre Bogus Andre "llogiq" Bogus is a Rust contributor and Clippy maintainer. A musician-turned-programmer, he has worked in many fields, from voice acting and teaching, to programming and managing software projects. He enjoys learning new things and telling others about them.

Recent posts:

Actix Web Adoption Guide: Overview, Examples, And Alternatives

Actix Web adoption guide: Overview, examples, and alternatives

Actix Web is definitely a compelling option to consider, whether you are starting a new project or considering a framework switch.

Eze Sunday
Mar 18, 2024 â‹… 8 min read
Getting Started With NativeWind: Tailwind For React Native

Getting started with NativeWind: Tailwind for React Native

Explore the integration of Tailwind CSS with React Native through NativeWind for responsive mobile design.

Chinwike Maduabuchi
Mar 15, 2024 â‹… 11 min read
Developing A Cross Platform Tv App With React Native

Developing a cross-platform TV app with React Native

The react-tv-space-navigation library offers a comprehensive solution for developing a cross-platform TV app with React Native.

Emmanuel Odioko
Mar 14, 2024 â‹… 10 min read
Essential Tools For Implementing React Panel Layouts

Essential tools for implementing React panel layouts

Explore some of the best tools in the React ecosystem for creating dynamic panel layouts, including react-resizable-layout and react-resizable-panels.

David Omotayo
Mar 13, 2024 â‹… 8 min read
View all posts

2 Replies to "Understanding inheritance and other limitations in Rust"

  1. “You could surely argue that even in the OO world, inheritance has a bad reputation and practitioners usually favor composition if they can.”

    I’ve been programming with OO for more than 25 years and I’ve never heard it had bad reputation, or that one should favor composition (which doesn’t offer the benefits of inheritance).

    When programming UI there is simply no alternative, and the lack of inheritance in Rust excludes it from any form of efficient UI framework. The default implementation on undefined types that you mention is not inheritance:
    – it cannot act on the object since it’s generic
    – a custom implementation can replace the default one, but not call it
    – there is only one level (replacing the default implementation)

    In general like any other feature, inheritance shouldn’t be abused, maybe that’s what you’re referring to, though without an example it is hard to tell for sure.

  2. You wrote, about code duplication due to the lack of inheritance: “The simplest way is, obviously, to duplicate the methods. Yes, duplication is bad. So is complexity. Create a free method and call that from the Cat and Lion impl if you need to deduplicate the code.”

    But how do you manage to access the struct’s (or worse, enum’s) fields from a free function, for several different types? Polymorphism doesn’t stretch that far, so it doesn’t look like this is possible and that’s the whole point about reusing methods with inheritance.

    That’s why I generally avoid using fake code with animals to talk about OOP, they don’t reflect the actual complexity and the interdependencies of the actual code.

    Currently one option is to use macros, like delegate for instance, but macros are not standard, harder to read and even harder to use with refactoring. That could be an acceptable temporary work-around if the macro was part of the core library, but it isn’t, which makes it a hack at best.

Leave a Reply