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

1 //Static array slice: no capacity. Reserve relocates.
2 int[4] sarray = [1, 2, 3, 4];
3 int[]  slice  = sarray[];
4 auto u = slice.reserve(8);
5 assert(u >= 8);
6 assert(&sarray[0] !is &slice[0]);
7 assert(slice.capacity == u);
8 
9 //Dynamic array slices
10 int[] a = [1, 2, 3, 4];
11 a.reserve(8); //prepare a for appending 4 more items
12 auto p = &a[0];
13 u = a.capacity;
14 a ~= [5, 6, 7, 8];
15 assert(p == &a[0]);      //a should not have been reallocated
16 assert(u == a.capacity); //a should not have been extended
Suggestion Box / Bug Report