liveBook · Manning

Slice

As explained in Systems Programming with Zig

A slice is a view representing a portion of a contiguous sequence of elements, typically through a pointer and a length.

Need one function to work with arrays of different lengths? In Zig, a slice carries a pointer to multiple elements and a length, which can be known at runtime. It references data stored elsewhere rather than containing the elements itself. section 2.2.3 section 2.2.2

A slice type is written []T, or []const T for a const slice. Index it to read or, where permitted, store elements. Its ptr field is a many-item pointer, and its len field is a usize. In safe mode, enabled by default, an out-of-bounds access produces an error and stack trace. section 2.2.3

Listing 2.10 Slices
const std = @import("std");

pub fn main() void {
    const arr = [_]i32{ 11, 22, 33, 44, 55 };    #1
    const slice: []const i32 = &arr;    #2
    const x = slice[2];    #3

    std.debug.print("slice: {any}, x: {}\n", .{slice, x});
    std.debug.print("ptr: {*}, len: {}\n", .{slice.ptr, slice.len});
}

A pointer to an array coerces to a slice spanning the whole array; the example indexes it and prints its pointer and length. section 2.2.3

Listing 2.11 Slices
const std = @import("std");

pub fn main() void {
    const arr = [_]i32{ 11, 22, 33, 44, 55 };
    const range: []const i32 = arr[1..4]; #1

    std.debug.print("range: {any}\n", .{range});
}

Slice a range of an array with arr[1..4]; the resulting view contains 22, 33, and 44. section 2.2.3

A many-item pointer, written [*]T or [*]const T, also supports indexing, but the compiler does not know how many elements it points to. The programmer must track that count to avoid invalid memory access. A slice adds a convenient length and safety checks; a many-item pointer can be useful when storing a length is undesirable. section 2.2.3

Slices also turn up at practical boundaries:

  • Input: takeDelimiter returns a string slice into a reader’s internal buffer, so the input need not be stored separately in this case. section 4.1.4
  • C interop: a sequence can cross the interface as separate pointer and length fields. A caller-provided pointer and length can also be combined into a slice for a fixed-buffer allocator. section 5.4.2
Listing 5.18 Extern recipes
const RetrievedIngredient = extern struct { #1
    quantity: [*:0]const u8,
    name: [*:0]const u8,
};

const RetrievedRecipe = extern struct { #1
    name: [*:0]const u8,
    author: [*:0]const u8,
    instructions: [*:0]const u8,
    ingredients: [*]const RetrievedIngredient, #2
    ingredient_count: usize, #2
};

The C-compatible recipe representation splits its ingredient sequence into a many-item pointer and an ingredient count. section 5.4.2

How Systems Programming with Zig covers it

How deep it goes
Explains slice types, array coercion, fields, indexing, bounds checks, and the trade-off with many-item pointers.
Examples
Uses an integer-array range, a reader’s input buffer, and a C-compatible recipe representation.
What you can do after
Apply slices in Zig to index arrays, handle runtime-length sequences, and work with pointer-and-length interfaces.
How it connects
Connects slices to arrays, pointers, bounds checking, buffers, allocators, and C interoperability.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.