mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
single pointer slice syntax added
This commit is contained in:
parent
01dc0d5a72
commit
9232425b8f
2 changed files with 15 additions and 0 deletions
|
|
@ -1935,6 +1935,7 @@ or
|
|||
<li>{#syntax#}*T{#endsyntax#} - single-item pointer to exactly one item.
|
||||
<ul>
|
||||
<li>Supports deref syntax: {#syntax#}ptr.*{#endsyntax#}</li>
|
||||
<li>Supports slice syntax: {#syntax#}ptr[0..1]{#endsyntax#}</li>
|
||||
<li>Supports pointer subtraction: {#syntax#}ptr - ptr{#endsyntax#}</li>
|
||||
</ul>
|
||||
</li>
|
||||
|
|
|
|||
|
|
@ -32,4 +32,18 @@ test "pointer array access" {
|
|||
try expect(array[2] == 4);
|
||||
}
|
||||
|
||||
test "slice syntax" {
|
||||
// Get a pointer to a variable:
|
||||
var x: i32 = 1234;
|
||||
const x_ptr = &x;
|
||||
|
||||
// Convert to array pointer using slice syntax:
|
||||
const x_array_ptr = x_ptr[0..1];
|
||||
try expect(@TypeOf(x_array_ptr) == *[1]i32);
|
||||
|
||||
// Coerce to many-item pointer:
|
||||
const x_many_ptr: [*]i32 = x_array_ptr;
|
||||
try expect(x_many_ptr[0] == 1234);
|
||||
}
|
||||
|
||||
// test
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue