alt.hn

6/3/2026 at 1:29:03 PM

Nine Ways to Do Inheritance in Rust, a Language Without Inheritance

https://medium.com/@carlmkadie/nine-ways-to-do-inheritance-in-rust-a-language-without-inheritance-14825bf1e215

by pjmlp

6/6/2026 at 7:24:42 AM

I almost never use inheritance beside using some kind of interfaces/traits to declare a contract.

However, the only time where I’m missing code/data reuse through inheritance is with GUI. Some mostly flat hierarchies of widgets are really powerful ways to declare and compose UI components with shared behaviors.

In rust, the DX for GUI components is always lacking compared to web, C#. With maybe the exception of Slint which is really not Rust anymore.

Is there a way to have good DX for GUI components in Rust?

by iTokio

6/6/2026 at 8:21:17 AM

If you want to get inspired by good component DX, try looking at Bevy, the game engine.

But essentially it comes down to traits, newtypes/enum variants, and macros.

by aabhay

6/6/2026 at 10:30:44 AM

I did have a look at bevy ECS approach and find it very verbose and really foreign, it’s in « not rust anymore» territory. Macros are a dangerous tool in terms of long term maintainability and are hurting compilation times.

But it’s still really fresh, they are conscious of the issues and I hope bevy maintainers came up with an elegant design

by iTokio

6/6/2026 at 10:52:24 AM

Right. Bevy has its own run time allocation and dependency system, and it's <<not rust anymore>>. It bypasses Rust's compile time ownership system using unsafe code. The encapsulation may be safe due to run time checking. It's bothersome that such things seem to be needed.

by Animats

6/6/2026 at 12:41:04 PM

> It's bothersome that such things seem to be needed.

Yes. Rust is not a general purpose language. It's a systems language. Don't use it for GUIs and games and such until somebody has figured out how to do it properly.

by amelius

6/6/2026 at 1:27:01 PM

All systems languages before Rust (granted, it's not a very long list) were successfully used for GUIs, games, embedded stuff and much more

by usrnm

6/6/2026 at 11:31:56 AM

Rust is great for acyclic data structures, but ... in a GUI you always have closures that point back to the widgets they were created for. That's one reason you should probably not use Rust for GUIs and similar problem areas.

by amelius

6/6/2026 at 5:02:23 PM

Why would that be a problem in rust?

by CyberDildonics

6/6/2026 at 9:47:59 AM

Isn't composition (react style) better for GUIs anyways?

by h4x0rr

6/6/2026 at 10:51:14 AM

That’s a good point.

Usually when people say to prefer composition over inheritance, that does not apply to the inheritance found in GUI, but as an alternative way to share behaviors as a property, « has a » instead of « is a », like has a role instead of being a user, admin..

That do not work well with GUI, where hundreds of of components are reusing common containers, widget behavior, it would be very verbose and painful to always declare common behaviors, data.

I think, React is cheating because it’s based on the web which already provide DOM node components and JavaScript, a GC language where most rust issues do not apply, there is already an underlying composition framework, the problem is that it is not easy to reproduce in rust.

by iTokio

6/6/2026 at 5:32:30 AM

Cool catalogue, but it had nothing to do with inheritance. It’s more “nine ways to do polymorphism-adjacent things using ad-hoc polymorphism (“traits”) instead of subtype polymorphism (‘inheritance’)”

by pdpi

6/6/2026 at 6:23:14 AM

Just like you can do OO in any language - for example, you can do OO like C (or at least that's what GTK folks believe), Rust already has most runtime mechanisms to pretty much implement inheritance in a library or a coding pattern (as opposed to a language feature)

For a long time the most important thing missing was a better story around upcasting, but Rust 1.86 implemented the usual child-to-parent upcasting [0] expected in OO (with the Any trait doing the parent-to-child downcasting [1] in a way that's less error prone than most OO languages actually)

Also note that Firefox and Servo were a early proponents of getting more OO into Rust (I guess that's because they need to deal with the DOM? Not sure). Which doesn't mean literally having classes and inheritance, but rather, language features that can emulate those when needed.

Today I think the missing piece is fields in traits: a way for a vtable to store field offsets rather than just pointers to method; current practice is to add getters and setters to traits when needed (but that's "ugly"). Unfortunately discussion around this stalled [2]

[0]: https://blog.rust-lang.org/2025/04/03/Rust-1.86.0/#trait-upc...

[1]: https://doc.rust-lang.org/std/any/index.html

[2]: maybe the latest one is https://internals.rust-lang.org/t/fields-in-traits/6933/ but idk

by nextaccountic

6/6/2026 at 5:25:03 PM

> Today I think the missing piece is fields in traits: a way for a vtable to store field offsets rather than just pointers to method; current practice is to add getters and setters to traits when needed (but that's "ugly")

I've been wanting for a long time to re-implement Perl's Moo/Moose/Dios in Rust via macros. I think that would make for some really nice (but non-rust -looking) ways of doing all sorts of object hierarchy stuff

by alfiedotwtf

6/6/2026 at 6:41:27 AM

Yes. The polymorphism isn't bad. It's the inheritance of data that's the problem.

Rust traits can't have associated data. That's the big difference from objects.

It's also hard to have a parent-child relationship where the child can find the parent. That takes much fooling around with wordy combos of Rc, Weak, upgrade, etc.

This makes it hard to convert C++ programs, and C++ programmers, to Rust. It's quite possible to write 100% safe code under Rust's constraints, but it takes a major rethink from C++.

by Animats

6/6/2026 at 6:35:58 AM

Much of this post seems to focus on interface inheritance via traits, which is a well-known feature of Rust. It's weird though that it completely misses the generic typestate pattern, which is pretty unique to Rust as far as it goes and can be quite fairly described as OOP implementation inheritance in a type-theoretic trench coat.

by zozbot234

6/6/2026 at 12:43:49 PM

I know little about rust, but not having inheritance to me is a big benefit :)

by jmclnx