On Tue, 25 Mar 2014 21:35:46 +0000, Mark Isaacson wrote: > 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.
I believe char[] -> string conversion with to! will use this implementation: https://github.com/D-Programming-Language/phobos/blob/ master/std/conv.d#L823 So there should be no performance implications, but to!string is better self-documenting and flexible should you need to change the input type in future.