Systems and Networking · 14 min · free lesson

Stack and Heap

Ownership and storage location are separate questions; draw the handle and the allocation.

Core idea

A move transfers ownership; handle placement and optimized instructions are not language guarantees.

Lesson map: a Vec moved to a new binding with its as_ptr address unchanged, above the rule that a move transfers ownership.
The owned allocation is retained across this move; this does not measure emitted machine instructions.

Understand it

Storage location and ownership are separate. A Vec handle can be a local, a field inside another allocation or optimized away. For a nonempty u8 vector with allocated capacity, moving the Vec transfers its existing buffer; no fixed count of machine instructions or copied bytes follows.

A function call commonly creates a stack frame containing local values or handles whose sizes are known to the compiler. Heap allocation supports values whose storage needs to live independently of a particular stack frame or whose size is chosen at runtime. But ownership and physical location are separate questions. A Vec<T> value holds a pointer, length, and capacity as a small handle; its elements usually live in a heap allocation. Moving the Vec handle transfers ownership of that allocation without copying every element.

Cloning a Vec is different: it creates another allocation and copies or clones elements. A Box<T> puts a T allocation on the heap but has one owner. A String is a growable UTF-8 buffer with a small handle. A fixed array can live inside a stack frame, inside a heap allocation, or inside another struct depending on where its owner is stored. Do not claim that every dynamically sized value automatically goes to the heap or that stack memory is always faster for every use.

In an interview, draw the owner’s handle and the allocation, then trace a move, a borrow, and a clone. Explain when Drop releases the allocation. Mention recursion and stack-size limits if relevant, but keep the answer tied to the code at hand. If asked about performance, say that allocation patterns, cache locality, and copying cost matter more than a slogan about stack versus heap. Measure a realistic workload before changing representation.

A borrow does not move the allocation at all; it creates temporary access to the existing value. Use this contrast with move and clone in the diagram. Also mention that stack overflow is possible with deep recursion or large local arrays, even though stack allocation itself is cheap in the common case.

Interview follow-up: How do Vec::clone, a borrowed slice and a moved Vec differ? What changes if the Vec handle is itself a field in a heap allocation?

Read the code

fn main() {
    let before = vec![1u8, 2, 3];
    let allocation = before.as_ptr();
    let after = before;
    assert_eq!(after.as_ptr(), allocation);
    assert_eq!(after, [1, 2, 3]);
}

The owned allocation is retained across this move; this does not measure emitted machine instructions.

Practice starter

fn main() {
    let a = vec![7u8; 1024];
    let pointer = a.as_ptr();
    let b = a;
    assert_eq!(b.as_ptr(), pointer);
    assert_eq!(b.len(), 1024);
}

Practise this code in Playground ↗

Try it

Say where each part of a Vec<String> lives and what a move copies.

What success looks like: You can explain why moving a String is cheap but cloning it is not.

Check your understanding

What does moving this Vec guarantee about its elements and allocation?

Reveal the answer
  1. The move must copy exactly three machine words as observable work.
  2. Ownership transfers without cloning every element or allocating a second buffer.
  3. Each element is copied into a newly allocated destination buffer.

Vec has pointer, length and capacity metadata, but its field layout/order and emitted move instructions are not fixed here. The handle need not be physically on a stack; optimization may eliminate a move.

Go deeper

Common misconception: Each element is copied into a newly allocated destination buffer.

A move leaves the buffer where it is, as the as_ptr assertion shows. Only clone allocates a new buffer and duplicates the elements.

  1. Identify the contract: Ownership and storage location are separate questions; draw the handle and the allocation.
  2. Trace the concrete example: The owned allocation is retained across this move; this does not measure emitted machine instructions.
  3. Test the changed constraint: How do Vec::clone, a borrowed slice and a moved Vec differ? What changes if the Vec handle is itself a field in a heap allocation?

Transfer challenge: How do Vec::clone, a borrowed slice and a moved Vec differ? What changes if the Vec handle is itself a field in a heap allocation?

Connections

  • Cancellation Safety: Dropping a future mid-await
  • Virtual Memory: Pages, faults, and resident size
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Stack and Heap (courseId "interview", lessonId "q-stack-heap")
Link: https://crabrix.com/learn/interview/q-stack-heap/
Goal: A move transfers ownership; handle placement and optimized instructions are not language guarantees.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-stack-heap) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.