MiddleTheoryOccasionalNot answered yet
How do you protect a slice from sharing a backing array with another?
Make an independent copy with copy(dst, src) into a freshly allocated slice, or cap capacity with the three-index expression s[low:high:max] so the next append must reallocate instead of overwriting shared elements. Return copies, not sub-slices, from APIs so callers cannot mutate your backing array.
- ✗Confusing pass-by-pointer with copying — a pointer still reaches the same backing array
- ✗Forgetting the third index —
s[low:high]keeps the parent's capacity, soappendcan still alias - ✗Returning a sub-slice from an API, letting callers mutate the internal backing array
- →Why does
copyrequire a destination of sufficient length rather than capacity? - →How does
s[low:high:max]differ froms[low:high]for the nextappend?
</content>