View source code
Display the source code in std/array.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.array.Appender/appender - multiple declarations

Function appender

Convenience function that returns a RefAppender instance initialized with arrayPtr. Don't use null for the array pointer, use the other version of appender instead.

RefAppender!(E[]) appender(P, E) (
  P arrayPtr


int[] a = [1, 2];
auto app2 = appender(&a);
writeln(app2[]); // [1, 2]
writeln(a); // [1, 2]
app2 ~= 3;
app2 ~= [4, 5, 6];
writeln(app2[]); // [1, 2, 3, 4, 5, 6]
writeln(a); // [1, 2, 3, 4, 5, 6]

assert(app2.capacity >= 5);

Function appender

Convenience function that returns an Appender instance, optionally initialized with array.

Appender!A appender(A)()
if (isDynamicArray!A);

Appender!(E[]) appender(A, E) (
  auto ref A array


auto w = appender!string;
// pre-allocate space for at least 10 elements (this avoids costly reallocations)
assert(w.capacity >= 10);

w.put('a'); // single elements
w.put("bc"); // multiple elements

// use the append syntax
w ~= 'd';
w ~= "ef";

writeln(w[]); // "abcdef"

Struct Appender

Implements an output range that appends data to an array. This is recommended over array ~= data when appending many elements because it is more efficient. Appender maintains its own array metadata locally, so it can avoid global locking for each append where capacity is non-zero.

struct Appender(A)
if (isDynamicArray!A);


this Constructs an Appender with a given array. Note that this does not copy the data. If the array has a larger capacity as determined by arr.capacity, it will be used by the appender. After initializing an appender on an array, appending to the original array will reallocate.


capacity[get] size_t
data[get] inout(ElementEncodingType!A)[]Use opSlice() from now on.
opSlice[get] inout(ElementEncodingType!A)[]


clear Clears the managed array. This allows the elements of the array to be reused for appending.
put Appends item to the managed array. Performs encoding for char types if A is a differently typed char array.
put Appends an entire range to the managed array. Performs encoding for char elements if A is a differently typed char array.
reserve Reserve at least newCapacity elements for appending. Note that more elements may be reserved than requested. If newCapacity <= capacity, then nothing is done.
shrinkTo Shrinks the managed array to the given length.
toString Gives a string in the form of Appender!(A)(data).


opOpAssign Appends to the managed array.


A the array type to simulate.

See Also



auto app = appender!string();
string b = "abcdefg";
foreach (char c; b)
writeln(app[]); // "abcdefg"

int[] a = [ 1, 2 ];
auto app2 = appender(a);
app2.put([ 4, 5, 6 ]);
writeln(app2[]); // [1, 2, 3, 4, 5, 6]


Andrei Alexandrescu and Jonathan M Davis


Boost License 1.0.