rustvectorallocation

rust increase the capacity of a vector by n


I'm working on leetcode problem 88 https://leetcode.com/problems/merge-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150 which merges a vector into another vector. Therefore the first vector is extended by a known amount. However, as some calculations need to be performed when merging the two vectors it's not possible to simply use an iterator and .extend().

Therefore I am looking for a way to to increase the capacity of the vector by a known amount (or two a known value) similar to how Vec::with_capacity() can be used to allocate a vector with a known capacity. Thanks,


Solution

  • You can use reserve to extend the capacity of an existing vector.