Avoid subtracting form .length

matheus matheus at gmail.com
Thu Nov 14 11:52:21 UTC 2024


On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote:
> ...

My version:

import std.stdio, std.array, std.range, std.algorithm;

void foo(ref int[] a, int filler){
    int[5] b;
    auto len = min(b.length,a.length);
    b[0..len] = a[0..len];
    b[len..5] = filler;
    writeln("a = ", a);
    writeln("b = ", b);
}

void main(){
    int[] a;

    a = iota(2).array;
    foo(a, 77);

    writeln("");

    a = iota(11).array;
    foo(a, 77);
}

output:

a = [0, 1]
b = [0, 1, 77, 77, 77]

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [0, 1, 2, 3, 4]

Matheus.


More information about the Digitalmars-d-learn mailing list