Skip to content

Commit cb77bd6

Browse files
Pyrolisticalandrewrk
authored andcommitted
[docs] add examples of array/slice init using result location
1 parent f71f27b commit cb77bd6

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

doc/langref/test_arrays.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ const mem = @import("std").mem;
55
// array literal
66
const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' };
77

8+
// alternative initialization using result location
9+
const alt_message: [5]u8 = .{ 'h', 'e', 'l', 'l', 'o' };
10+
11+
comptime {
12+
assert(mem.eql(u8, &message, &alt_message));
13+
}
14+
815
// get the size of an array
916
comptime {
1017
assert(message.len == 5);

doc/langref/test_basic_slices.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
const expect = @import("std").testing.expect;
2+
const expectEqualSlices = @import("std").testing.expectEqualSlices;
23

34
test "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);

0 commit comments

Comments
 (0)