authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-05-02 12:51:58+10:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-05-07 15:55:21+10:00
log5bb8e9cd975ea68c924d04be6608f96b41204b7f
tree61c6c3510f3d75f40c557cc6571c4bf2e94a873a
parent64e319f5555a25dbf4c4d7e65289f7f96d6588b4

langref: mention slice-by-length pattern


1 files changed, 8 insertions(+), 0 deletions(-)

doc/langref.html.in+8
...@@ -2953,6 +2953,14 @@ test "basic slices" {...@@ -2953,6 +2953,14 @@ test "basic slices" {
2953 const array_ptr = array[0..array.len];2953 const array_ptr = array[0..array.len];
2954 try expect(@TypeOf(array_ptr) == *[array.len]i32);2954 try expect(@TypeOf(array_ptr) == *[array.len]i32);
29552955
2956 // You can perform a slice-by-length by slicing twice. This allows the compiler
2957 // to perform some optimisations like recognising a comptime-known length when
2958 // the start position is only known at runtime.
2959 var runtime_start: usize = 1;
2960 const length = 2;
2961 const array_ptr_len = array[runtime_start..][0..length];
2962 try expect(@TypeOf(array_ptr_len) == *[length]i32);
2963
2956 // Using the address-of operator on a slice gives a single-item pointer,2964 // Using the address-of operator on a slice gives a single-item pointer,
2957 // while using the `ptr` field gives a many-item pointer.2965 // while using the `ptr` field gives a many-item pointer.
2958 try expect(@TypeOf(slice.ptr) == [*]i32);2966 try expect(@TypeOf(slice.ptr) == [*]i32);