Convert this Vec: [1, 2, 3, 4, 5, 6] to: [2, 3, 4] without cloning anything.
Ideally, a method like vec.resize_at(index, length) or something would be nice.
I guess with a signature of something like pub fn resize_at(self, index: usize, length: usize) -> Vec<T>, where it consumes the original Vec but doesn't clone it.
Oh, cool, thanks! I'll definitely use this, but out of curiosity, there's no way to give back the old memory to the OS? (Coming from C, I'm thinking about realloc)
BTW, No matter how you do it, you can't change the start position without cloning or moving the elements.
[1, 2, 3, 4, 5, 6] to [1, 2, 3, 4] is cheap and easy with truncate(). But Vec holds the pointer to the start of the data, and it must be the pointer that is used to free the memory. Giving pointer+1 to the allocator would crash it, and Vec doesn't have room to remember that +1 offset. So the only way to move the start is to copy or reallocate.
You can take a slice [1..4] at zero cost, leaving Vec's mem as-is. And if you want to free the remaining memory, there's hardly any difference between truncate/drain/shrink_to_fit combos and just vec[1..4].to_owned().