Relatively often you need to insert a slice of data (usually bytes) into a vec at some arbitrary index. Sadly std doesn't have any optimized and short method to do exactly that. Currently you will have to write:
vec.reserve(slice.len()); // optional, but increases the performance let mut v = vec.split_off(index); vec.extend_from_slice(&slice); vec.append(&mut v);
This solution isn't slow, but not too nice to use and could be faster.
I'd propose adding a method which can insert a slice of
Copyable elements into a vec.
I have come up with this code to do exactly that.
According to my benchmarks the custom (unsafe) solution is about 50% faster. Take my benchmarks with a grain of salt though, they aren't perfect.
What do you think about that? Are there better solutions or names?