logoalt Hacker News

gignicoyesterday at 10:10 PM1 replyview on HN

> Under the hood, a virtual table (vtable) is created for each class, and a pointer (vptr) to the vtable is added to each instance.

Coming from C++ I assumed this was the only way but Rust has an interesting approach where the single objects do not pay any cost because virtual dispatch is handled by fat pointers. So you carry around the `vptr` in fat pointers (`&dyn MyTrait`) only when needed, not in every instance.


Replies

dalvrosayesterday at 10:18 PM

Good point, thanks for sharing!