Library reference

Read an item by its position

Pick an item using its position in the list. Here we read the second item every time.

array indexing

Simple example

Read the second number.

Start4 · 7 · 9
Result7
lesson.zig
const std = @import("std");

pub fn main() !void {
    const values = [_]i64{4, 7, 9};
    std.debug.print("{any}\n", .{values[1]});
}
Output
7
Advertisements

Everyday examples

Which ticket is second in the queue?

Start42 · 17 · 23
Result17
lesson.zig
const std = @import("std");

pub fn main() !void {
    const values = [_]i64{42, 17, 23};
    std.debug.print("{any}\n", .{values[1]});
}
Output
17

Which stop does the bus visit second?

Start8 · 6 · 2
Result6
lesson.zig
const std = @import("std");

pub fn main() !void {
    const values = [_]i64{8, 6, 2};
    std.debug.print("{any}\n", .{values[1]});
}
Output
6

What was the second thermometer reading?

Start19 · 21 · 20
Result21
lesson.zig
const std = @import("std");

pub fn main() !void {
    const values = [_]i64{19, 21, 20};
    std.debug.print("{any}\n", .{values[1]});
}
Output
21
Run the example
Terminal
zig run lesson.zig
Official documentation