16.8 Object Safety: What Makes a Trait Usable as a Trait Object
Alright, let’s talk about the bouncer at the Rust club: Object Safety. This is the rule that decides whether your trait gets to be a star on the main stage (as a dynamic trait object) or if it’s stuck performing in the compile-time-only static lounge. You see, when you use a trait object (&dyn SomeTrait), you’re telling the compiler, “I don’t know the exact type here at compile time, just that it implements this behavior.” This is dynamic dispatch. The compiler’s job is to make this work by creating a vtable—a little structure of function pointers—for each concrete type that implements the trait. The trait object itself is then a fat pointer: one pointer to the actual data, and one pointer to the appropriate vtable for its type.