Indexing and Iteration

A slice reaches its elements exactly as an array does — by position or in a loop — because both share the same element access. What is unique to a slice is that the access goes through its data pointer into memory it does not own.

Indexing

Use [i] to read or write a single element. The index is zero-based, so valid indices run from 0 to length - 1:

func Highlight(line: Slice<char8>) -> char8 {
    return line[0];    // first character
}

let s = "cyan";
let c = s[0];          // 'c'

A write through a slice updates the underlying memory the slice views — the slice does not hold its own copy, so the change is visible through every other slice or array over the same region.

Iteration

for … in walks every element in order, binding each to the loop variable. It reads length from the header, so it always covers exactly the elements the slice describes:

func Sum(values: Slice<int32>) -> int32 {
    var total = 0;
    for v in values {
        total += v;
    }
    return total;
}

See for for the full loop syntax.

Spread

Pass all elements of a slice as individual arguments to a variadic function with ...:

func Max(values: int32...) -> int32 { /* ... */ }

let data: Slice<int32> = collect();
let m = Max(data...);   // each element becomes one argument

See Variadic Functions for how the receiving function declares and reads its arguments.

See Also