Commit cb77bd672c

Pyrolistical <pyrogx1133@gmail.com>
2024-05-07 20:00:42
[docs] add examples of array/slice init using result location
1 parent f71f27b
Changed files (2)
doc/langref/test_arrays.zig
@@ -5,6 +5,13 @@ const mem = @import("std").mem;
 // array literal
 const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' };
 
+// alternative initialization using result location
+const alt_message: [5]u8 = .{ 'h', 'e', 'l', 'l', 'o' };
+
+comptime {
+    assert(mem.eql(u8, &message, &alt_message));
+}
+
 // get the size of an array
 comptime {
     assert(message.len == 5);
doc/langref/test_basic_slices.zig
@@ -1,10 +1,17 @@
 const expect = @import("std").testing.expect;
+const expectEqualSlices = @import("std").testing.expectEqualSlices;
 
 test "basic slices" {
     var array = [_]i32{ 1, 2, 3, 4 };
     var known_at_runtime_zero: usize = 0;
     _ = &known_at_runtime_zero;
     const slice = array[known_at_runtime_zero..array.len];
+
+    // alternative initialization using result location
+    const alt_slice: []const i32 = &.{ 1, 2, 3, 4 };
+
+    try expectEqualSlices(i32, slice, alt_slice);
+
     try expect(@TypeOf(slice) == []i32);
     try expect(&slice[0] == &array[0]);
     try expect(slice.len == array.len);