If a trait has a supertrait you can coerce a reference to said trait object to a reference to a trait object of the supertrait
As someone that just started learning Rust: wha?
livingcoder@programming.dev
on 08 Apr 14:28
collapse
Basically, you can generalize your trait types into their parent (super) traits for situations when functionality is specific to those supertrait objects.
As an example, if you have a trait CanBark and it is a super trait for the trait IsDog, you can coerce your references of &dyn IsDog into a &dyn CanBark. You can then work with other trait types that share a super trait.
threaded - newest
Wow, that trait feature is great. I’ve been eagerly waiting for that one for a long time. Thank you to everyone who made that possible.
As someone that just started learning Rust: wha?
Basically, you can generalize your trait types into their parent (super) traits for situations when functionality is specific to those supertrait objects.
As an example, if you have a trait CanBark and it is a super trait for the trait IsDog, you can coerce your references of &dyn IsDog into a &dyn CanBark. You can then work with other trait types that share a super trait.
At least, I hope this is possible now. If it’s purely “you can return a coerced type from a function”, that is less useful.
Thank you
So basically, it’s like inheritance but for traits?
Exactly. The functions of the super trait are also required when implementing the child trait’s functions, as you would expect from inheritance.
This is kinda neat. I was hoping for something more generic, so hopefully this is a step toward that.
This seems huge for library authors! That’s not me as of yet, but maybe soon. 😀
Congrats on the release!