Rust Reference ยท Core Traits
The Sized trait and ?Sized
Reference for Rust Sized and ?Sized: statically sized types, dynamically sized types (str, slices, dyn Trait), implicit generic bounds, fat pointers and size_of_val.
Sized marks types whose size is known at compile time. Almost every type you write is Sized: integers, structs, enums, arrays, references and Box<T> itself. The compiler needs the size to put a value on the stack, pass it to a function or store it in a variable.
Types without a compile-time size are dynamically sized types (DSTs): str, slices [T] and trait objects dyn Trait. You can never hold one by value; you use them behind a pointer such as &str, Box<[T]> or &dyn Trait. Those pointers are "fat" and carry extra data (a length or a vtable pointer).
fn show<T: ?Sized + std::fmt::Debug>(value: &T) { println!("{value:?}"); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
pub trait Sized {} // a marker trait, implemented by the compiler fn f<T>(x: T) {} // means: T: Sized fn g<T: ?Sized>(x: &T) {} // opt out of the implicit bound struct Wrapper<T: ?Sized> { value: Box<T> }
| Part | Description |
|---|---|
T: Sized |
Implicit on every generic type parameter. You never write it. |
T: ?Sized |
"May or may not be sized." The only ? bound in Rust. |
Self: Sized |
An explicit bound, often placed on a trait method to keep the trait usable as dyn Trait. |
Errors / panics / edge cases: E0277 "the size for values of type str cannot be known at compilation time" appears when you pass or return a DST by value, or use it for a plain generic parameter.
Usage notes
- Every generic parameter is
Sizedby default. That is whyfn f<T>(x: &T)rejectsstrand[T]unless you add?Sized. - Traits are different: in a trait declaration
Selfis?Sizedby default, which is what allowsdyn Traitand impls forstr. A default method that takesselfby value therefore needswhere Self: Sized. - Where DSTs appear:
str,[T],dyn Trait, and custom structs whose last field is a DST. Values live behind&,&mut,Box,RcorArc. - Fat pointers are two words: address plus length for
str/[T], address plus vtable fordyn Trait. - Arrays are sized, slices are not.
[i32; 3]isSizedand coerces to&[i32]by unsizing. - Local unsized values (
let x: [i32] = ...) are not allowed on stable Rust, and parameters of unsized type are rejected. size_of_valmeasures the real size of the pointee, including for DSTs;size_of::<T>()needsT: Sized.- Rule of thumb: write
?Sizedon a generic only when your function takes&T,Box<T>or similar and genuinely supportsstr, slices or trait objects.
Related
dyn,strslicing, Slices,SendandSync- Lessons: Trait objects, Memory layout, Generics