Report #7629
[bug\_fix] the size for values of type \`str\` cannot be known at compilation time \(E0277\)
Add the \`?Sized\` bound to the generic parameter: \`fn process\(t: &T\)\` or \`struct Wrapper\(Box\)\`. Alternatively, change the usage site to use a reference or \`Box\` \(e.g., \`&str\` or \`Box\` instead of \`str\`\). This fixes the root cause because Rust requires all generic type parameters to have a known size at compile time by default \(the \`Sized\` trait is implicit\); \`str\` and \`\[T\]\` are Dynamically Sized Types \(DSTs\) whose size is only known at runtime, so they must be used behind a pointer and the generic must explicitly opt-out of \`Sized\` with \`?Sized\`.
Journey Context:
Developer is designing a serialization wrapper that should work with any type, including unsized ones like string slices or byte arrays. They define \`struct Serializer\(T\);\` and attempt to implement methods for it. When they try to use \`Serializer\` or \`Serializer<\[u8\]>\`, the compiler emits an error that the size of \`str\` cannot be known at compilation time. The developer first tries to use \`Box\`, which is sized \(a fat pointer\), but their generic \`Serializer\` is used in many places expecting \`T\` to be sized. They realize that the implicit \`Sized\` bound on generics is the culprit. After reading the Advanced Types chapter in the Rust Book, they learn about Dynamically Sized Types \(DSTs\) and the \`?Sized\` opt-out syntax. They modify their struct to \`struct Serializer\(Box\);\`, allowing it to accept \`str\` and \`\[u8\]\` behind a Box, while still accepting sized types like \`i32\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T03:17:53.860394+00:00— report_created — created