{
  "id": 5824175,
  "title": "Visualizing Rust's Vtables: How dyn Trait Works In Memory",
  "url": "https://urgent.news/2026/09/05/visualizing-rusts-vtables-how-dyn-trait-works-in-memory-5824175",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-05T13:31:05.000Z",
  "source": {
    "name": "Hacker News",
    "slug": "hacker-news",
    "url": "https://sofiabelen.github.io/projects/visualizing-rusts-vtables-how-dyn-trait-works-in-memory/"
  },
  "original_language": "en",
  "account": "Rust's equivalent to C++ virtual functions is dyn Trait, which enables runtime polymorphism. To understand this concept, we'll first explore an alternative approach to solve the same problem in C++, namely the CRTP (Curiously Recurring Template Pattern). CRTP is a compile-time polymorphism technique where the compiler generates separate functions for each type it's called with. Unlike C++, Rust offers monomorphization, which generates a separate copy of the function for each type, but with zero runtime cost.\n\nWhen comparing Rust and C++'s approach to polymorphism, it's essential to understand that treating Rust as C++ with different syntax does not provide a revolutionary perspective. Rust has its unique philosophy and features that make it distinct from C++.\n\nRust implements static dispatch through generics, providing compile-time polymorphism similar to CRTP. This approach sacrifices readability for efficiency, as the compiler generates separate functions for each type it's called with. This method ensures no runtime cost but requires types to be known at compile time.\n\nHowever, there are scenarios where static dispatch is not sufficient, and dynamic dispatch becomes necessary. One such example is creating a Vec<T> that holds different types of shapes. In this case, static dispatch cannot be applied directly because the types are not known at compile time.\n\nTo illustrate the difference between static and dynamic dispatch, we'll examine the memory layout of our objects. The &dyn Draw type is twice the size of a regular pointer because it consists of two pointers: one points to the data, and the other points to a vtable. The vtable contains information about which draw() method to call at runtime. By inspecting the memory layout, we can see that objects of the same type share a vtable, while the data pointer changes per instance.\n\nIn summary, Rust's dyn Trait enables runtime polymorphism and offers a more straightforward and simple alternative to CRTP, achieving compile-time polymorphism through monomorphization. While static dispatch (generics) is sufficient in some cases, dynamic dispatch is necessary when dealing with scenarios like creating a Vec<T> that holds different types of shapes.",
  "summary": null,
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 2,
    "also_reported_by": [
      {
        "outlet": "Lobsters",
        "title": "Visualizing Rust's Vtables: How dyn Trait Works In Memory",
        "url": "https://urgent.news/2026/09/05/visualizing-rusts-vtables-how-dyn-trait-works-in-memory",
        "published": "2026-09-05T11:50:24.000Z"
      }
    ]
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}