The Sized trait indicates a type has a known size at compile time, which is required for stack allocation and generic parameters by default.
The Sized trait marks types with a known size at compile time, allowing them to be stored directly on the stack. It is implicitly required for all generic type parameters unless explicitly removed with ?Sized.
fn generic<T: Sized>(t: T) {
// T has a known size at compile time
}
fn generic_maybe_sized<T: ?Sized>(t: &T) {
// T might be unsized (e.g., str, dyn Trait)
}
The Sized trait tells the compiler that a type has a fixed, known size in memory. This matters because the computer needs to know exactly how much space to reserve for a variable before the program runs. Think of it like packing a suitcase: you can easily pack a shirt (known size), but you can't pack a cloud (unknown size) without a specific container.