View source code
Display the source code in std/range.d from which this page was generated on
github.
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.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Function std.range.takeOne
Returns a range with at most one element; for example,
returns a range consisting of the integer takeOne
([42, 43, 44])42
. Calling popFront()
off that range renders it empty.
In effect
is somewhat equivalent to takeOne
(r)
but in
certain interfaces it is important to know statically that the range may take
(r, 1)only
have at most one element.
The type returned by
is a random-access range with length
regardless of takeOne
R
's capabilities (another feature that distinguishes
from takeOne
).
take
Prototype
auto takeOne(R)( R source ) if (isInputRange!R);
Example
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);
Authors
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.