Slices

A slice, written Slice<T>, is a view over a contiguous sequence of elements. It does not own the elements — it only records where they live and how many there are. The storage belongs to something else: an array, a heap allocation, or a static in the binary.

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

Because a slice is only a view, passing one is cheap regardless of how many elements it spans — the elements are never copied with it.

Memory Layout

A slice value is a fat pointer: a fixed 16-byte header of two fields, whatever the element type or count.

Offset  Size  Field
0       8     data:   *T       — pointer to the first element
8       8     length: uint64   — number of elements

The header is 8-byte aligned, since its first field is a pointer. The element data it points at is stored separately and is not part of these 16 bytes. Both fields can be read directly:

let nums = [10, 20, 30];   // int[3] — an array
let view: Slice<int> = nums;    // a slice viewing it
let p = view.data;         // *int — pointer to the first element
let n = view.length;       // uint64 — 3

This is the essential contrast with an array: an array is its elements (N × sizeof(T) bytes stored inline), while a slice merely points at elements owned elsewhere.

Where Slices Come From

SourceExample
Viewing an arraylet v: Slice<int32> = arr;
Sub-slicing with a rangelet mid = arr[1..4];
A string literallet s = "Hello"; // Slice<char8>
A data pointer and length from native codesee Slices and Pointers

A slice is valid only while the memory it views is alive. A slice into a local array dangles once that array goes out of scope.

Further Topics

TopicDescription
String LiteralsThe Slice<char8> slice a "..." literal produces
Indexing and IterationReading, writing, looping, and spreading elements
Slices and PointersLow-level access through data, sizeof, and extern

See Also

  • Arrays — the owning T[N] storage a slice most often views
  • Pointers — the *T type behind a slice's data field
  • Variadic Functions — functions that accept a variable number of arguments