Ranges in use
The key concept to understand ranges in the general case is that, unless they are infinite, they are consumable. In idiomatic usage, they aren't intended to be kept around, adding and removing elements to and from them as if they were some sort of container. A range is generally created only when needed, passed to an algorithm as input, then ultimately consumed, often at the end of a chain of algorithms. Even forward ranges and output ranges with their save
and put
primitives usually aren't intended to live beyond an algorithm.
That's not to say it's forbidden to keep a range around; some might even be designed for long life. For example, the random number generators in std.random
are all ranges that are intended to be reused. However, idiomatic usage in D generally means lazy, fire-and-forget ranges that allow algorithms to operate on data from any source and minimize memory allocations.
For most programs, the need to deal with ranges directly should be rare; most code will...