authorgravatar for pyrogx1133@gmail.comPyrolistical <pyrogx1133@gmail.com> 2024-05-07 11:00:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-10 13:56:01-07:00
logcb77bd672c3b398e3c5f6be80af03243bf8638e3
tree3941236c60db642437683f492badae8209790ed0
parentf71f27bcb0ceb851a7c95f3970d644623b8d67ba

[docs] add examples of array/slice init using result location


2 files changed, 14 insertions(+), 0 deletions(-)

doc/langref/test_arrays.zig+7
......@@ -5,6 +5,13 @@ const mem = @import("std").mem;
55// array literal
66const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' };
77
8// alternative initialization using result location
9const alt_message: [5]u8 = .{ 'h', 'e', 'l', 'l', 'o' };
10
11comptime {
12 assert(mem.eql(u8, &message, &alt_message));
13}
14
815// get the size of an array
916comptime {
1017 assert(message.len == 5);
doc/langref/test_basic_slices.zig+7
......@@ -1,10 +1,17 @@
11const expect = @import("std").testing.expect;
2const expectEqualSlices = @import("std").testing.expectEqualSlices;
23
34test "basic slices" {
45 var array = [_]i32{ 1, 2, 3, 4 };
56 var known_at_runtime_zero: usize = 0;
67 _ = &known_at_runtime_zero;
78 const slice = array[known_at_runtime_zero..array.len];
9
10 // alternative initialization using result location
11 const alt_slice: []const i32 = &.{ 1, 2, 3, 4 };
12
13 try expectEqualSlices(i32, slice, alt_slice);
14
815 try expect(@TypeOf(slice) == []i32);
916 try expect(&slice[0] == &array[0]);
1017 try expect(slice.len == array.len);