Library reference

Find an item in a list

Check whether a particular value appears anywhere in a list. A single match is enough.

std.mem.indexOfScalar

Simple example

Does the list contain two?

Start1 · 2 · 3
Resulttrue
lesson.zig
const std = @import("std");

pub fn main() !void {
    const values = [_]i64{1, 2, 3};
    std.debug.print("{any}\n", .{std.mem.indexOfScalar(i64, &values, 2) != null});
}
Output
true
Advertisements

Everyday examples

Is ticket 17 on the pickup list?

Start15 · 17 · 20
Resulttrue
lesson.zig
const std = @import("std");

pub fn main() !void {
    const values = [_]i64{15, 17, 20};
    std.debug.print("{any}\n", .{std.mem.indexOfScalar(i64, &values, 17) != null});
}
Output
true

Does this bus stop at stop 6?

Start2 · 4 · 8
Resultfalse
lesson.zig
const std = @import("std");

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

Is parcel 42 on the van?

Start42 · 18 · 15
Resulttrue
lesson.zig
const std = @import("std");

pub fn main() !void {
    const values = [_]i64{42, 18, 15};
    std.debug.print("{any}\n", .{std.mem.indexOfScalar(i64, &values, 42) != null});
}
Output
true
Run the example
Terminal
zig run lesson.zig
Official documentation