trait

Sized — types with compile-time known size

Most generic type parameters implicitly require Sized. Relax that requirement with ?Sized when a function only borrows a value. A str is dynamically sized; &str itself has a known size. This function accepts either a sized type or a dynamically sized string slice. Expected stdout (not executed): 4 5 Common mistake: Reading ?Sized as a requirement that the type must be unsized. Static Rust 2024 example; not a verified ByteMind executable assessment.

Formal Syntax & Structure

rust syntaxrust
T: Sized; T: ?Sized

Working Example

Example Snippetrust
fn bytes<T: ?Sized>(value: &T) -> usize { std::mem::size_of_val(value) }fn main() { println!("{} {}", bytes(&42u32), bytes("hello")); }
Rust Reference