liveBook · Manning

Heap

As explained in Learn Rust in a Month of Lunches

The heap is a region of memory used for dynamically allocated data whose size or lifetime is not limited to a call frame.

The heap is a region of memory for data whose size is not known at compile time or whose storage needs exceed a call frame. A program requests space there, while a fixed-size pointer on the stack records where the data lives. section 2.1

Heap memory differs from stack memory in several practical ways:

  • It can hold data of any size, unlike the stack, which needs each variable’s exact size at compile time. section 2.1
  • It is usually slower than the stack because the computer follows a pointer to reach the data. section 2.1
  • Small embedded devices may provide no heap because they have no operating system to request heap memory from. section 2.1

Known-size and variable-size data

An i32 always occupies four bytes, so it can live directly on the stack. Data whose size is unknown at compile time goes in the heap, while a stack pointer leads the computer to it. section 2.1

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines heap memory, contrasts its speed and sizing with the stack, and stops short of describing allocation internals.
Examples
Uses i32 versus unknown-sized data, a pointer-following analogy, and a small embedded device without heap access.
What you can do after
Understand when data moves to heap memory and how a stack pointer leads to it.
How it connects
Connects heap memory to the stack, pointers, references, variables, and ownership.

Also explained in

Compare how 7 books explain heap →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.