mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
langref: mention slice-by-length pattern
This commit is contained in:
parent
64e319f555
commit
5bb8e9cd97
1 changed files with 8 additions and 0 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue