3 ms·
I actually end up generally only using polymorphism in Rust and have never needed inheritance. For polymorphism, as other comments have mentioned, you should lo
by bluejekyll 5y ago
I actually end up generally only using polymorphism in Rust and have never needed inheritance. For polymorphism, as other comments have mentioned, you should look at trait objects (`&dyn T`) or generic parameters for monomorphism.
If absolutely necessary, Objects can be downcast by using Any (though I've personally not needed to write Rust code that needs this): https://doc.rust-lang.org/std/any/trait.Any.html https://doc.rust-lang.org/std/any/trait.Any.html. Edit, instead I tend to rely on Rust enums (for it's algebraic type features).
Also, in some cases you may end up wanting to implement Deref or DerefMut, https://doc.rust-lang.org/std/ops/trait.Deref.html https://doc.rust-lang.org/std/ops/trait.Deref.html, but this shouldn't be use to create inheritance, it's more used for getting references from one type to another similar type (like String derefs to &str for example).