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
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});
}
true
Advertisements
Everyday examples
Is ticket 17 on the pickup list?
Start15 · 17 · 20
Resulttrue
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});
}
true
Does this bus stop at stop 6?
Start2 · 4 · 8
Resultfalse
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});
}
false
Is parcel 42 on the van?
Start42 · 18 · 15
Resulttrue
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});
}
true