liveBook · Manning

Dynamically sized type

As explained in Rust in Action: Systems programming concepts and techniques

A dynamically sized type is a type whose size is not known at compile time and whose values require runtime size information.

A dynamically sized type has a size the compiler cannot determine at compile time. That does not necessarily mean its value grows or shrinks while the program runs. A slice, for example, can represent a fixed number of elements even though its type does not specify that number. section 6.3.2

A slice of an array

let values = [10, 20, 30]; let slice: &[i32] = &values[0..2]; The slice refers to the first two elements of the array. Its type, [i32], does not include a length; the reference carries a pointer and length information so the program knows how much of the array the slice represents. section 6.3.2 section 6.1

The heap is an area of memory used for data whose size is not known at compile time. Heap data is accessed through a pointer, but a slice can also refer to part of an existing array; it does not have to own heap data. section 6.3.2

String and Vec<T> provide a useful contrast: their contents can grow or shrink. Trait objects are another example of dynamically sized types; they allow different types to be used in the same container. section 6.3.2

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Defines dynamically sized types and explains slice references, while mentioning trait objects without developing them.
Examples
Uses slices of arrays and contrasts them with String and Vec<T>, whose contents can grow or shrink.
What you can do after
Evaluate whether an unknown compile-time size necessarily means a value changes size at runtime.
How it connects
Connects dynamically sized types to slices, trait objects, pointers, references, and heap memory.

Also explained in

Compare how 2 books explain dynamically sized type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.