Using Slices as OutputRange

You will get to know the use of slices as OutputRange in this lesson.

We'll cover the following

The std.range module makes slices OutputRange objects as well. In contrast, std.array makes them only input ranges. Unfortunately, using slices as OutputRange objects has a confusing effect: Slices lose an element for each put() operation on them; and that element is the one that has just been displayed!

The reason for this behavior is a consequence of slices’ not having a put() member function. As a result, the third case of the previous table is matched for slices, and the following method is applied:

range.front = e; 
range.popFront();

As the code above is executed for each put(), the front element of the slice is assigned to the value of the outputted element, to be subsequently removed from the slice with popFront():

Get hands-on with 1400+ tech skills courses.