const std = @import("std"); const expect = std.testing.expect; const mem = std.mem; const fmt = std.fmt; test "using slices for strings" { // Zig has no concept of strings. String literals are const pointers // to null-terminated arrays of u8, and by convention parameters // that are "strings" are expected to be UTF-8 encoded slices of u8. // Here we coerce *const [5:0]u8 and *const [6:0]u8 to []const u8 const hello: []const u8 = "hello"; const world: []const u8 = "世界"; var all_together: [100]u8 = undefined; // You can use slice syntax with at least one runtime-known index on an // array to convert an array into a slice. var start: usize = 0; _ = &start; const all_together_slice = all_together[start..]; // String concatenation example. const hello_world = try fmt.bufPrint(all_together_slice, "{s} {s}", .{ hello, world }); // Generally, you can use UTF-8 and not worry about whether something is a // string. If you don't need to deal with individual characters, no need // to decode. try expect(mem.eql(u8, hello_world, "hello 世界")); } test "slice pointer" { var array: [10]u8 = undefined; const ptr = &array; try expect(@TypeOf(ptr) == *[10]u8); // A pointer to an array can be sliced just like an array: var start: usize = 0; var end: usize = 5; _ = .{ &start, &end }; const slice = ptr[start..end]; // The slice is mutable because we sliced a mutable pointer. try expect(@TypeOf(slice) == []u8); slice[2] = 3; try expect(array[2] == 3); // Again, slicing with comptime-known indexes will produce another pointer // to an array: const ptr2 = slice[2..3]; try expect(ptr2.len == 1); try expect(ptr2[0] == 3); try expect(@TypeOf(ptr2) == *[1]u8); } // test