Report #30226
[bug\_fix] the size for values of type \`dyn Trait\` cannot be known at compilation time / the trait \`Sized\` is not implemented for \`\(dyn Trait \+ 'static\)\`
Use a pointer type like \`Box\`, \`&dyn Trait\`, or \`Rc\` to put the trait object behind a pointer \(fat pointer\), or change the generic bound to \`T: MyTrait \+ ?Sized\` if the function can handle unsized types. Root cause: Trait objects \(\`dyn Trait\`\) are dynamically sized types \(DSTs\) because the concrete type \(and thus size\) is only known at runtime; Rust requires all values to have known sizes at compile time for stack allocation, so DSTs must be behind a pointer indirection.
Journey Context:
Developer is refactoring code to use polymorphism for a UI system. They define \`trait Drawable \{ fn draw\(&self\); \}\` and try to store a vector of mixed types: \`let shapes: Vec = vec\!\[Circle, Square\];\`. The compiler immediately errors with "the size for values of type \`dyn Drawable\` cannot be known at compilation time". Developer is confused because they thought trait objects worked like interfaces in Java where you can store them directly in a list. They try \`Vec<&dyn Drawable>\` which works for references but they want owned data that can be moved between threads. They search and learn about \`Box\` which allocates the concrete type \(Circle, Square\) on the heap and stores a fat pointer \(data pointer \+ vtable pointer\) in the Vec. They change to \`Vec>\` and it compiles. Later they hit a similar error in a generic function \`fn process\(item: T\)\` where they try to cast \`item as dyn Drawable\` and learn about \`Box\` again or the \`?Sized\` bound for special generic cases.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T05:07:14.605494+00:00— report_created — created