How Rust Trait Objects Look Under the Hood

A fresh memory dissection shows why Rust’s approach to dynamic traits isn't just C++ with prettier syntax.
Developer Sofia Belén poked under the hood of Rust to see how virtual dispatch is laid out in system memory. While dissecting simple structs, she noticed a key surprise: zero-sized types (ZSTs) take up literally zero bytes of memory. Unlike C++, which forces empty objects to take up at least 1 byte for unique addresses, Rust tracks identity through its borrow checker at compile time. In debug builds, Rust assigns these empty types dummy 1-byte stack slots solely so debuggers can inspect them.
Why it matters: Seeing exact memory layouts makes Rust's zero-cost abstractions feel concrete instead of magical. By handling object identity at compile time rather than relying on unique byte addresses, Rust strips away memory overhead that C++ has carried for decades.
Here's the gist: if you try to learn Rust by drawing direct 1:1 parallels to C++, you miss the exact design choices that make Rust different.
Turns out comparing Rust to C++ is a trap — but a remarkably educational one.
Sources
- Visualizing Rust's Vtables — https://sofiabelen.github.io/projects/visualizing-rusts-vtables-how-dyn-trait-works-in-memory/
- Hacker News Discussion — https://news.ycombinator.com/item?id=49576343

