reserve

Reserves capacity for a slice. The capacity is the size that the slice can grow to before the underlying array must be reallocated or extended.

pure nothrow @trusted
size_t
reserve
(
T
)
(
ref T[] arr
,)

Return Value

Type: size_t

The new capacity of the array (which may be larger than the requested capacity).

Examples

//Static array slice: no capacity. Reserve relocates.
int[4] sarray = [1, 2, 3, 4];
int[]  slice  = sarray[];
auto u = slice.reserve(8);
assert(u >= 8);
assert(&sarray[0] !is &slice[0]);
assert(slice.capacity == u);

//Dynamic array slices
int[] a = [1, 2, 3, 4];
a.reserve(8); //prepare a for appending 4 more items
auto p = &a[0];
u = a.capacity;
a ~= [5, 6, 7, 8];
assert(p == &a[0]);      //a should not have been reallocated
assert(u == a.capacity); //a should not have been extended

Meta