4 ms·
This is a nice example to do inheritance the Rust-way using composition instead of OO, but still I'm missing some essential parts for which I have not found the
by zevv 2y ago
This is a nice example to do inheritance the Rust-way using composition instead of OO, but still I'm missing some essential parts for which I have not found the proper Rust idioms yet.
For example, when building a widget hierarchy, I would like to be able to pass widgets of different types to a function or store them in a `Vec<>`, but this is not possible since these are different concrete types.
Trait objects can help here, but then Rust lacks the machinery to upcast and downcast between types as there is no RTTI attached to these trait objects; you can not cast/convert a `Button` to a `BaseWidget`, or a `BaseWidget` that was originally a `Button` back to a `Button`.
What would be the rusty way to handle this?
- dwattttt 2y agoI think what you're looking for is std::any; https://doc.rust-lang.org/std/any/index.html https://doc.rust-lang.org/std/any/index.html The example there includes a function that takes Any + Debug, tries to downcast to a String, and if that succeeds it logs the string length