liveBook · Manning

Heap

As explained in Refactoring to Rust

A heap is a dynamically managed memory region for data whose size or lifetime is not confined to a function call.

A heap is dynamically managed memory for data whose size is unknown before execution or exceeds practical stack limits. Arrays and strings commonly use heap memory. section 2.2

Heap memory differs from stack memory in several practical ways:

  • Supports much larger allocations, limited mainly by the computer’s available memory rather than a small stack limit. section 2.2
  • Requires allocation to reserve an area and deallocation to make unused memory available again. section 2.2
  • Uses Rust’s term dropping for the cleanup process, while deallocation remains the generic term. section 2.2

Welcoming a visitor

A program allocates enough memory for a visitor’s name, prints “Welcome {name},” and eventually releases that memory for reuse. Manual cleanup can be placed after the function call or inside the function, as shown by the contrasting welcome examples. section 2.2

How Refactoring to Rust covers it

How deep it goes
Defines heap memory, contrasts it with the stack, and explains allocation and cleanup without descending into implementation internals.
Examples
Uses arrays, strings, a visitor-name program, and contrasting placements of manual cleanup.
What it assumes
Assumes basic program memory concepts while introducing stack-versus-heap storage, allocation, and deallocation.
What you can do after
Evaluate whether data needs dynamically managed memory and identify the allocation and cleanup stages.
How it connects
Connects heap memory with the stack, dynamic memory, allocation, deallocation, and Rust’s drop terminology.

Also explained in

Compare how 7 books explain heap →

Related concepts

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