commit cb77bd672c3b398e3c5f6be80af03243bf8638e3 (tree)
parent f71f27bcb0ceb851a7c95f3970d644623b8d67ba
Author: Pyrolistical <pyrogx1133@gmail.com>
Date: Tue, 7 May 2024 11:00:42 -0700
[docs] add examples of array/slice init using result location
Diffstat:
2 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/doc/langref/test_arrays.zig b/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);
diff --git a/doc/langref/test_basic_slices.zig b/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);