From 5bb8e9cd975ea68c924d04be6608f96b41204b7f Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreplay.github.com> Date: Tue, 2 May 2023 12:51:58 +1000 Subject: [PATCH] langref: mention slice-by-length pattern --- doc/langref.html.in | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/langref.html.in b/doc/langref.html.in index 69ad0624c4..5876fd79ba 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2953,6 +2953,14 @@ test "basic slices" { const array_ptr = array[0..array.len]; try expect(@TypeOf(array_ptr) == *[array.len]i32); + // You can perform a slice-by-length by slicing twice. This allows the compiler + // to perform some optimisations like recognising a comptime-known length when + // the start position is only known at runtime. + var runtime_start: usize = 1; + const length = 2; + const array_ptr_len = array[runtime_start..][0..length]; + try expect(@TypeOf(array_ptr_len) == *[length]i32); + // Using the address-of operator on a slice gives a single-item pointer, // while using the `ptr` field gives a many-item pointer. try expect(@TypeOf(slice.ptr) == [*]i32);