Dynamically sized types
Most types have a fixed size that is known at compile time and implement the trait Sized. A type with a size that is known only at run-time is called a dynamically sized type (DST) or, informally, an unsized type. Slices, trait objects, and str are examples of DSTs.
Such types can only be used in certain cases:
- Pointer types to DSTs are
sized but have twice the size of pointers to sized types
- Pointers to slices and
stralso store the number of elements. - Pointers to trait objects also store a pointer to a vtable.
- Pointers to slices and
- DSTs can be provided as
type arguments to generic type parameters having the special
?Sizedbound. They can also be used for associated type definitions when the corresponding associated type declaration has a?Sizedbound. By default, any type parameter or associated type has aSizedbound, unless it is relaxed using?Sized.
- Traits may be implemented for DSTs.
Unlike with generic type parameters,
Self: ?Sizedis the default in trait definitions.
- Structs may contain a DST as the last field; this makes the struct itself a DST.