1 //Static array slice: no capacity 2 int[4] sarray = [1, 2, 3, 4]; 3 int[] slice = sarray[]; 4 assert(sarray.capacity == 0); 5 //Appending to slice will reallocate to a new array 6 slice ~= 5; 7 assert(slice.capacity >= 5); 8 9 //Dynamic array slices 10 int[] a = [1, 2, 3, 4]; 11 int[] b = a[1 .. $]; 12 int[] c = a[1 .. $ - 1]; 13 debug(SENTINEL) {} else // non-zero capacity very much depends on the array and GC implementation 14 { 15 assert(a.capacity != 0); 16 assert(a.capacity == b.capacity + 1); //both a and b share the same tail 17 } 18 assert(c.capacity == 0); //an append to c must relocate c.
(Property) Gets the current capacity of a slice. The capacity is the size that the slice can grow to before the underlying array must be reallocated or extended.
If an append must reallocate a slice with no possibility of extension, then 0 is returned. This happens when the slice references a static array, or if another slice references elements past the end of the current slice.
Note: The capacity of a slice may be impacted by operations on other slices.