3 ms·
The article references using trait objects for dynamic dispatch. FWIW, I found them extremely limited: you can't use generics, can't return Self... I wonder how
by allengeorge 9y ago
The article references using trait objects for dynamic dispatch. FWIW, I found them extremely limited: you can't use generics, can't return Self... I wonder how often people use them.
- cbcoutinho 9y agoWhat do you mean you can't return self? I'm just starting out with learning rust, but from what I've gathered you can use objects in a myriad of ways that will allow you do what you need. Instead of returning self, you could just pass a reference - you need to mutate self then just pass a mutable reference. Did I miss something here?
- steveklabnik 9y agoReturning Self in a trait object isn't possible; it's not object safe. In order to return Self, you'd need to know the concrete type of the thing, and when you have a trait object, that's erased. Returning a trait object instead of plain Self would work though.