Steven Schveighoffer Wrote: > * shrinkToFit(T[] arr): This one is a bit tricky and technically unsafe. > It reduces the size of the "allocated" length to fit the length of the > array. The allocated length is the length that the runtime assumes is > being used of the memory block. This is what aids in preventing stomping, > so use with care. You can achieve stomping if you use shrinkToFit on a > slice, but still have references to the trimmed data. Example: > > string s = "12345".idup; > string slice = s[0..2]; > slice.shrinkToFit(); // tells the runtime that you will no longer use any > data beyond the slice > slice ~= "00"; // appends in place, overwriting immutable data referenced
Actually, slice.capacity *increases* from 0 to s.capacity when calling shrinkToFit, doesn't it? So "stomp" or "prepare_for_stomping" could be a better name.