Urgent.News

What's breaking now, across thousands of outlets.

Tech

Visualizing Rust's Vtables: How dyn Trait Works In Memory

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.

When 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++.

Rust 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.

However, 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.

To 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.

In 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.

Written by urgent.news from Hacker News's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

This story

This is one outlet's version. Read the fullest account.

Read the original at sofiabelen.github.io →

More in Tech

I automated Dependabot PR cleanup — and drew a hard line on what NOT to automate

If you use GitHub, you know the rhythm: Dependabot opens a PR, CI runs green, and then a human has to answer the same questions again — is this a patch or a major? did anyone touch source code?

  • Developed CLI tool dep-triage to categorize open Dependabot PRs
  • Categorizes PRs into auto-merge, escalate, close, rebase suggestion, skip
  • Limits automation to dependency-only diffs, green CI, dependency-file changes

More from Saturday 5 September →