Skip to content
Learn Netverks

Lesson

Step 17/36 47% through track

stack-heap

Stack and heap

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Stack and heap: the syntax, APIs, and habits you need before advancing in Rust.

Teams ship Stack and heap on every Rust codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Stack and heap in contexts like: Infrastructure CLIs, proxies, game engines, blockchain nodes, and latency-sensitive backends.

Write Rust with fn main(), click Run on server—the dev runner compiles main.rs with rustc and runs the binary; fix borrow errors from stderr (requires Rust toolchain; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Rust places fixed-size data on the stack and dynamically sized data (like String contents) on the heap. Ownership tracks who frees heap allocations.

Stack vs heap

  • Stack — fast LIFO; locals like i32, pointers with known size
  • Heap — flexible size; String, Vec, Box allocate here

Compare with C++

Like C++, you control performance—but Rust's ownership prevents many manual memory bugs by default.

Important interview questions and answers

  1. Q: Where does a String's metadata live?
    A: Stack struct pointing at heap buffer for UTF-8 bytes.

Self-check

  1. Which is faster to allocate: stack or heap?
  2. Who frees heap memory in safe Rust?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Box when?
  • Stack overflow?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump