Search code examples
ddmd

How to output the contents of a DList as an array to the console?


I'm just started to learn Dlang.

Need to output DList!int as an array - [1, 2, 3].

import std.stdio : writeln;
import std.container.dlist : DList;

void main()
{
    DList!int list;
    list.insertFront(1);
    list.insertBack([2, 3]);
    writeln(list); // prints DList!int(7F50A689A000) 
}

Solution

  • You were very close. You just needed the [] to make a Range out of it, and then the writeln() line would work as you expected:

    writeln(list[]); // prints [1, 2, 3]