Is there a more elegant way to do this in D?

Meta jared771 at gmail.com
Thu Apr 8 18:01:56 UTC 2021


On Thursday, 8 April 2021 at 12:19:29 UTC, WebFreak001 wrote:
>
> ```d
> string to01String(int[] x) @safe
> {
>     auto conv = x.to!(ubyte[]); // allocates new array, so 
> later cast to string is OK
>     conv[] += '0'; // assume all numbers are 0-9, then this 
> gives the correct result
>     return (() @trusted => cast(string)conv)();
> }
> ```

The @trusted lambda can also be replaced with 
[std.exception.assumeUnique](https://dlang.org/library/std/exception/assume_unique.html).


More information about the Digitalmars-d-learn mailing list