tezvyn:

Explain Go escape analysis and Rust ownership for stack vs heap

AI-drafted, machine-checkedSource: doc.rust-lang.orgadvanced

Tests compiler-driven memory placement. Go escape analysis keeps non-escaping locals on stack, shrinking heap and GC work. Rust ownership lets the compiler pick stack or heap at build time with zero cost.

RED FLAG

Saying Go eliminates GC or Rust uses one.

WHAT THIS TESTS: This question tests whether you understand the boundary between compile-time and runtime memory management, specifically how a language can avoid or reduce garbage collection overhead by proving at build time that a value does not need to outlive its stack frame. It also checks if you can compare two fundamentally different strategies: Go's compiler-assisted optimization within a garbage-collected runtime versus Rust's ownership system that eliminates the need for a tracing garbage collector entirely.

A GOOD ANSWER COVERS: First, define Go escape analysis as a compile-time pass that examines pointer flow and variable lifetimes to decide whether an allocation can live on the stack or must move to the heap. If a local variable is not returned, stored in a global, or referenced by a closure that outlives the function, the compiler can allocate it on the stack, which means the garbage collector never sees it and does not need to trace or mark it. Second, explain the concrete benefit: reducing heap size and the mark phase workload, which directly lowers GC pause pressure and CPU overhead, though it does not remove the GC itself. Third, pivot to Rust and describe how ownership, borrowing, and the rule that stack data must have a known fixed size allow the compiler to deterministically choose the stack whenever possible. Because the borrow checker enforces that there is exactly one owner and that references cannot outlive the owned value, Rust does not need a runtime collector to decide when memory is freed; drop semantics are deterministic. Fourth, explicitly contrast the two: Go uses escape analysis as an optimization within a GC'd world, while Rust uses ownership as a semantic guarantee that makes heap versus stack allocation a compile-time contract with zero runtime tracing cost.

COMMON WRONG ANSWERS: A major red flag is claiming that Go escape analysis eliminates the garbage collector or that Go programs do not use heap allocation. Another is stating that Rust uses a garbage collector or that ownership is merely a nicer syntax for manual malloc and free. Candidates also err by saying escape analysis is a runtime process; it happens during compilation. Finally, avoid asserting that Rust never uses the heap; Box and Vec are explicit heap allocations that ownership still manages deterministically.

LIKELY FOLLOW-UPS: An interviewer may ask how you can inspect escape analysis decisions in Go, such as using go build with the gcflags -m option. They might ask what kinds of code patterns force heap allocation in Go, like returning pointers to locals, interface conversions, or variable-sized slices. In Rust, expect questions about how Rc or Arc shift memory management from compile-time to runtime reference counting, or how the borrow checker handles self-referential structs.

ONE CONCRETE EXAMPLE: Consider a function that creates a small byte buffer and sums its contents. In Go, if the buffer is declared as a local array and no pointer escapes, the compiler stack-allocates it; the GC ignores it entirely during a collection cycle. In Rust, the same local array has a fixed size known at compile time, so it sits on the stack by default. If you instead need a growable vector, you use Vec, which goes on the heap, but ownership ensures it is freed exactly when the owning scope ends without a GC cycle.

Read the original → doc.rust-lang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.