Inserting multiple items into a Vec efficiently?


#1

I can’t find an efficient way to insert multiple items into the start/middle of a Vec efficiently. Right now if I have a large Vec and I want to add a couple of items at the start, I need to insert each element separately, making the expensive move of the later elements - twice. Ideally, I should be able to insert a Vec/slice/iterator so that the resizing should happen once.

I wanted to make sure I wasn’t missing an easy solution before I opened an issue.


#3

Here’s a solution in a custom crate, method .splice(range, iterable) to insert at any point in the vector.

VecExt in crate odds, (odds @ crater.io)

It’s been discussed briefly on the rfcs repo, no strong push to add it.

It would be helpful for an eventual RFC if you have constructive criticism of that splice implementation.


#4

Thanks! That is exactly what I was looking for. I’ll add a comment to the RFC, thank you.