|
| 1 | +# Dynamically Sized Types |
| 2 | + |
| 3 | +Most types have a fixed size that is known at compile time and implement the |
| 4 | +trait [`Sized`][sized]. A type with a size that is known only at run-time is |
| 5 | +called a _dynamically sized type_ (_DST_) or (informally) an unsized type. |
| 6 | +[Slices] and [trait objects] are two examples of <abbr title="dynamically sized |
| 7 | +types">DSTs</abbr>. Such types can only be used in certain cases: |
| 8 | + |
| 9 | +* [Pointer types] to <abbr title="dynamically sized types">DSTs</abbr> are |
| 10 | + sized but have twice the size of pointers to sized types |
| 11 | + * Pointers to slices also store the number of elements of the slice. |
| 12 | + * Pointers to trait objects also store a pointer to a vtable. |
| 13 | +* <abbr title="dynamically sized types">DSTs</abbr> can be provided as |
| 14 | + type arguments when a bound of `?Sized`. By default any type parameter |
| 15 | + has a `?Sized` bound. |
| 16 | +* Traits may be implemented for <abbr title="dynamically sized |
| 17 | + types">DSTs</abbr>. Unlike type parameters`Self: ?Sized` by default in trait |
| 18 | + definitions. |
| 19 | +* Structs may contain a <abbr title="dynamically sized type">DST</abbr> as the |
| 20 | + last field, this makes the struct itself a |
| 21 | + <abbr title="dynamically sized type">DST</abbr>. |
| 22 | + |
| 23 | +Notably: [variables], function parameters, [const] and [static] items must be |
| 24 | +`Sized`. |
| 25 | + |
| 26 | +[sized]: the-sized-trait.html |
| 27 | +[Slices]: #array-and-slice-types |
| 28 | +[trait objects]: #trait-objects |
| 29 | +[Pointer types]: #pointer-types |
| 30 | +[variables]: variables.html |
| 31 | +[const]: items.html#constant-items |
| 32 | +[static]: items.html#static-items |
0 commit comments