zlacker

[parent] [thread] 0 comments
1. brabel+(OP)[view] [source] 2024-01-24 11:49:25
Wow that seems pretty unsafe...

In D, for example, this works as most people would expect:

    import std.stdio;
    void main() {
      int[] a = [1, 2, 3, 4, 5];
      auto lo = a[0 .. 2], hi = a[2 .. $];
      lo ~= [6,7,8]; // new allocation
      writefln("%s %s", lo, hi);  // prints [1, 2, 6, 7, 8] [3, 4, 5]
    }
You simply can't overwrite a backing array from a slice (unless you do unsafe stuff very explicitly).
[go to top]