Operations on All Elements

In this lesson, we will see operations on all elements of an array. This feature is for both fixed-length arrays and slices.

Operations on all elements of an array #

The [] characters written after the name of an array refers to all elements of the array. This feature simplifies the program when certain operations need to be applied to all of the elements of the array.

Press + to interact
import std.stdio;
void main() {
double[3] a = [ 10, 20, 30 ];
double[3] b = [ 2, 3, 4 ];
double[3] result = a[] + b[];
writeln(result);
}

The addition operation in this program is applied to the corresponding elements of both of the arrays in order: the first ...