Dynamically Sized Type

In Rust, most types have a size known at compile time - an  is 8 bytes, a   is 4 bytes, and so on. These types all implement the auto trait. However, not all types do - some types have a size that is only known at runtime. These types are called unsized types, dynamically sized types, or DSTs.

There are four kinds of DSTs in Rust:
 * Trait objects . As they can be any type, their size is dynamic.
 * Slices . They can contain any number of elements.
 * The string slice type . String slices can contain any number of bytes.
 * Structs and tuples that contain a DST.

As DSTs are unsized, they can't be held on the stack like regular variables – you must store a pointer to them; this can be a reference or a container like or.

Pointers and references to DSTs in Rust are "fat" – they are twice as wide as a regular pointer, because they contain more information than just their memory address. Trait objects also store a pointer to the virtual method table, other DSTs store their length in addition to their memory address.

At the moment, DSTs are very hard to work with mostly because they can't be stored on the stack, which prevents owning or moving a DST without allocating it on the heap. This will hopefully get better in the future – at the moment, try to avoid them.

Dynamically sized structs and tuples
Structs and Tuples are allowed to contain at most one DST. If they do, they are dynamically sized as well. The DST must be the very last field in the struct or tuple. For example: In this example, the field  is sized, since   is behind a reference. However,, and in effect  , can be unsized. For example,  is unsized.

Sized bounds
Because DSTs are so difficult to work with, all generics implicitly require. To either require unsized types or to support both you can write  or   respectively; for example   has   which allows it to store both sized and unsized values.