[Style] Converting from char[] to string, to!string vs idup

Mark Isaacson turck11 at hotmail.com
Tue Mar 25 14:35:46 PDT 2014


I am presently working my way through TDPL for the second time, 
and there's an example in chapter 1 to the effect of:

[code]
string currentParagraph;
foreach(line; stdin.byLine()) {
   if (line.length > 2) {
     currentParagraph = to!string(line[2 .. $]);
   }
}
[/code]

The explicit conversion is necessary because `line` is a `char[]` 
but `currentParagraph` is a `string`. My question is why use 
`to!string` instead of just doing `line[2 .. $].idup`?

Is there a performance benefit? Is it simply because it's more 
general?

I tried taking a peek at the implementation of to!string, but it 
wasn't easy enough to follow and boil down into components.


More information about the Digitalmars-d-learn mailing list