Report #66720
[bug\_fix] the size for values of type \`dyn Trait\` cannot be known at compilation time
Use a pointer type like \`Box\`, \`&dyn Drawable\`, or \`Arc\` to provide the indirection required for dynamically sized trait objects.
Journey Context:
Developer is building a game engine. They define \`trait Drawable \{ fn draw\(&self\); \}\` and implement it for \`Circle\` and \`Square\`. They want a list of all drawable objects: \`let shapes: Vec = vec\!\[Circle::new\(\), Square::new\(\)\];\`. The compiler errors with \`the size for values of type 'dyn Drawable' cannot be known at compilation time\`. The developer tries \`Vec<&dyn Drawable>\` but struggles with lifetime annotations when storing the vector in a struct. They try \`Vec\` again, confused because traits are types too. They search "rust vector of different types" and learn about trait objects. They try \`Box::new\(Circle\)\` but the Vec type is \`Vec>\`, which is homogeneous and doesn't allow mixing Circle and Square. They finally understand that \`Box\` is a fat pointer \(data pointer \+ vtable pointer\) with a known size \(16 bytes on 64-bit\), allowing it to be stored in a Vec. They change the type to \`Vec>\` and push \`Box::new\(Circle::new\(\)\)\` and \`Box::new\(Square::new\(\)\)\`. This compiles because the Box provides the indirection required for the dynamically sized type, and the compiler can generate code to dispatch the \`draw\` method via the vtable at runtime.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T18:27:59.197112+00:00— report_created — created