Dyn dispatch
Dyn dispatch is a method call through a vtable: the function pointer is loaded at runtime from the trait object, not baked in at compile time.
Detailed Explanation
Static dispatch is a direct call. The compiler knows the type, knows the function address, and can inline. Dyn dispatch is an indirect call. The compiler knows only the trait. At runtime it loads a function pointer from the vtable that travels with the dyn Trait pointer and jumps there. The concrete type is not in the caller's source.
The vtable is a static table the compiler builds per (Type, Trait) pair. It holds function pointers for each of the trait's methods, plus a destructor, size, and alignment so Box<dyn Trait> can drop and deallocate the value without knowing the type. Creating a trait object, box as Box<dyn Draw>, stores a pointer to that table next to a pointer to the value.
The performance story is the same as virtual calls in C++. One extra load and an indirect jump. That is cheap compared to I/O and compared to a missed cache, and expensive compared to an inlined two-instruction method. In a tight numeric loop, dyn dispatch also blocks some optimisation: the compiler cannot see through the call to constant-fold or vectorise. In a GUI paint loop of a few hundred widgets, you will not measure it.
Dyn dispatch is what you asked for when you wrote dyn. A generic T: Draw never uses a vtable. impl Draw in argument position never uses a vtable. People say "traits are virtual" because they are thinking of Java interfaces; Rust traits are virtual only behind dyn.
Devirtualisation exists. If the compiler can prove the concrete type — you created the box and called it in the same function and it did not escape — it may replace the vtable call with a direct one. Do not rely on it. If the call must be fast, keep the generic.
Object safety is the gate. A trait that is not object-safe has no vtable the compiler is willing to emit, so dyn dispatch cannot happen. Split the trait, or stay generic.
When you choose dyn dispatch: mixed collections, plugin boundaries, compile-time cost of monomorphising a huge generic, or an API that should not mention T. When you choose static dispatch: the default. Measure before you rewrite a generic as dyn for speed; you usually make it slower.
Code Examples
trait Speak {
fn speak(&self);
}
struct Dog;
impl Speak for Dog {
fn speak(&self) {
println!("woof");
}
}
fn static_speak<T: Speak>(s: &T) {
s.speak(); // compiled as a direct call for each T
}
fn dyn_speak(s: &dyn Speak) {
s.speak(); // vtable call
}
fn main() {
static_speak(&Dog); // prints: woof
dyn_speak(&Dog); // prints: woof
}Common Gotchas
Dyn is not free in a hot loop
An indirect call plus a lost inline can be a double-digit percent in numeric code. Keep those paths generic.
Generics never go through a vtable
`fn f<T: Trait>(x: T)` is static dispatch even though a trait is involved. `dyn` is the keyword that changes that.
A vtable call cannot see extra methods
Methods on the concrete type that are not on the trait are unreachable through `dyn Trait`. Downcast with `Any` only when you own the design.
Fat pointer plus vtable is two words of reference
Passing `&dyn Trait` is still a copy of two pointers, not a clone of the value.