4 ms·
no, you can have "trait objects" in Rust, which are boxed and use dynamic dispatch instead of static dispatch, but they lose the original type (type erasure --
by untothebreach 11y ago
no, you can have "trait objects" in Rust, which are boxed and use dynamic dispatch instead of static dispatch, but they lose the original type (type erasure -- another similarity with java :)), so they aren't true generics.
For example, you could use the `Read` trait as a type:
fn some_func(a: Box<Read>) { }
but then only the `Read` methods would be available on `a`, you wouldn't be able to access methods from the original type of `a`.
- geodel 11y agoThanks. I need to spend time and learn Rust. The one thing stopping me is lack of some small work related project which I can try in Rust
- solidsnack9000 11y ago> so they aren't true generics. Why does type erasure mean they aren't true generics?
- whyever 11y agoThey don't lose anything compared to monomorphized templates. You can only access methods of the trait in both cases.