Curriculum Memory Ownership conventions exercise 2 · mcq
Ownership conventions
TypeScript functions that READ a collection just receive the
reference. Zig functions that READ a buffer they don't own
take a SLICE ([]const T for read-only). The function
doesn't allocate, doesn't free, doesn't keep the slice past
return. Pick the right signature.
About this theme
Zig has no borrow checker — ownership is convention, not enforcement. The rule that makes manual memory livable: whoever calls init is responsible for calling deinit. A function that allocates and returns ownership uses errdefer to clean up on failure; the caller then defers the deinit. A function that borrows takes a slice or pointer and never frees. Once these conventions become reflex, manual memory feels almost as easy as garbage collection — minus the runtime cost.