Function std.range.takeOne
Returns a range with at most one element; for example, takeOne([42, 43, 44])
returns a range consisting of the integer 42
. Calling popFront()
off that range renders it empty.
auto auto takeOne(R)
(
R source
)
if (isInputRange!R);
In effect takeOne(r)
is somewhat equivalent to take(r, 1)
but in
certain interfaces it is important to know statically that the range may only
have at most one element.
The type returned by takeOne
is a random-access range with length
regardless of R
's capabilities, as long as it is a forward range.
(another feature that distinguishes takeOne
from take
). If
(D R) is an input range but not a forward range, return type is an input
range with all random-access capabilities except save.
Example
auto s = takeOne([42, 43, 44]);
static assert(isRandomAccessRange!(typeof(s)));
writeln(s .length); // 1
assert(!s .empty);
writeln(s .front); // 42
s .front = 43;
writeln(s .front); // 43
writeln(s .back); // 43
writeln(s[0]); // 43
s .popFront();
writeln(s .length); // 0
assert(s .empty);
Authors
Andrei Alexandrescu, David Simcha, Jonathan M Davis, and Jack Stouffer. Credit for some of the ideas in building this module goes to Leonardo Maffi.