Iterators and Ranges: Comparing C++ to D to Rust

Ali Çehreli acehreli at yahoo.com
Mon Jun 14 13:30:52 UTC 2021


On 6/14/21 5:33 AM, IGotD- wrote:

 > The narrator in the video claims that in D popFront doesn't consume the
 > element. Is this true? When I do a popFront, doesn't it consume the
 > first element in an array for example. Can someone clarify this?

popFront() consumes the range, not the container:

import std.range;

void main() {
   auto arr = [ 1, 2, 3, 4 ];
   auto half = arr[0..$/2];
   half.popFront();
   assert(arr[0] == 1);  // Nothing happened to the array (which 'arr'
                         // is just another slice of it)
}

Ali



More information about the Digitalmars-d mailing list