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.

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.

takeOne
(
R
)
()
if (
isInputRange!R
)

Examples

auto s = takeOne([42, 43, 44]);
static assert(isRandomAccessRange!(typeof(s)));
assert(s.length == 1);
assert(!s.empty);
assert(s.front == 42);
s.front = 43;
assert(s.front == 43);
assert(s.back == 43);
assert(s[0] == 43);
s.popFront();
assert(s.length == 0);
assert(s.empty);

Meta