takeExactly

Similar to take, but assumes that range has at least n elements. Consequently, the result of takeExactly(range, n) always defines the length property (and initializes it to n) even when range itself does not define length.

The result of takeExactly is identical to that of take in cases where the original range defines length or is infinite.

Unlike take, however, it is illegal to pass a range with less than n elements to takeExactly; this will cause an assertion failure.

takeExactly
(
R
)
(,
size_t n
)
if (
isInputRange!R
)

Examples

import std.algorithm.comparison : equal;

auto a = [ 1, 2, 3, 4, 5 ];

auto b = takeExactly(a, 3);
assert(equal(b, [1, 2, 3]));
static assert(is(typeof(b.length) == size_t));
assert(b.length == 3);
assert(b.front == 1);
assert(b.back == 3);

Meta