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;...@@ -5,6 +5,13 @@ const mem = @import("std").mem;
5// array literal5// array literal
6const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' };6const 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
8// get the size of an array15// get the size of an array
9comptime {16comptime {
10 assert(message.len == 5);17 assert(message.len == 5);
doc/langref/test_basic_slices.zig+7
...@@ -1,10 +1,17 @@...@@ -1,10 +1,17 @@
1const expect = @import("std").testing.expect;1const expect = @import("std").testing.expect;
2const expectEqualSlices = @import("std").testing.expectEqualSlices;
23
3test "basic slices" {4test "basic slices" {
4 var array = [_]i32{ 1, 2, 3, 4 };5 var array = [_]i32{ 1, 2, 3, 4 };
5 var known_at_runtime_zero: usize = 0;6 var known_at_runtime_zero: usize = 0;
6 _ = &known_at_runtime_zero;7 _ = &known_at_runtime_zero;
7 const slice = array[known_at_runtime_zero..array.len];8 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
8 try expect(@TypeOf(slice) == []i32);15 try expect(@TypeOf(slice) == []i32);
9 try expect(&slice[0] == &array[0]);16 try expect(&slice[0] == &array[0]);
10 try expect(slice.len == array.len);17 try expect(slice.len == array.len);