Report #91999
[bug\_fix] the size for values of type \`dyn Drawable\` cannot be known at compilation time
Wrap the trait object in a \`Box\` \(heap-allocated pointer\) or use \`&dyn Drawable\` \(borrowed reference\) to create a sized pointer type that can be stored in variables, struct fields, or vectors.
Journey Context:
Developer defines \`trait Drawable \{ fn draw\(&self\); \}\` with implementors \`Circle\` and \`Square\`. They try to create a heterogeneous collection: \`let shapes: Vec = vec\!\[Circle \{\}, Square \{\}\];\`. Compiler errors with "the size for values of type \`dyn Drawable\` cannot be known at compile time". Developer learns that \`dyn Trait\` is a dynamically sized type \(DST\) because different implementors have different sizes \(Circle might be 8 bytes, Square 16 bytes\). They try \`Vec<&Drawable>\` but still get DST errors because references to trait objects are fat pointers \(data pointer \+ vtable pointer\), but the Vec expects sized elements. They discover that wrapping in \`Box\` creates a smart pointer with a fixed size \(just a pointer on the stack\) that points to the heap-allocated trait object. They change to \`let shapes: Vec> = vec\!\[Box::new\(Circle \{\}\), Box::new\(Square \{\}\)\];\`. This works because \`Box\` is a sized type \(a fat pointer behind another pointer, effectively sized\). They also learn \`&dyn Trait\` works for borrowed trait objects but requires lifetime management.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T13:00:43.168577+00:00— report_created — created