mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Doc: elaborate on Slice section
somehow the concept of slices had less elaboration in its own dedicated section than the mentions of it elsewhere.
This commit is contained in:
parent
1c85b0acbb
commit
aecdf6ad50
1 changed files with 6 additions and 4 deletions
|
|
@ -2990,15 +2990,17 @@ pub fn main() anyerror!void {
|
||||||
{#header_close#}
|
{#header_close#}
|
||||||
|
|
||||||
{#header_open|Slices#}
|
{#header_open|Slices#}
|
||||||
|
<p>
|
||||||
|
A slice is a pointer and a length. The difference between an array and
|
||||||
|
a slice is that the array's length is part of the type and known at
|
||||||
|
compile-time, whereas the slice's length is known at runtime.
|
||||||
|
Both can be accessed with the `len` field.
|
||||||
|
</p>
|
||||||
{#code_begin|test_safety|test_basic_slices|index out of bounds#}
|
{#code_begin|test_safety|test_basic_slices|index out of bounds#}
|
||||||
const expect = @import("std").testing.expect;
|
const expect = @import("std").testing.expect;
|
||||||
|
|
||||||
test "basic slices" {
|
test "basic slices" {
|
||||||
var array = [_]i32{ 1, 2, 3, 4 };
|
var array = [_]i32{ 1, 2, 3, 4 };
|
||||||
// A slice is a pointer and a length. The difference between an array and
|
|
||||||
// a slice is that the array's length is part of the type and known at
|
|
||||||
// compile-time, whereas the slice's length is known at runtime.
|
|
||||||
// Both can be accessed with the `len` field.
|
|
||||||
var known_at_runtime_zero: usize = 0;
|
var known_at_runtime_zero: usize = 0;
|
||||||
const slice = array[known_at_runtime_zero..array.len];
|
const slice = array[known_at_runtime_zero..array.len];
|
||||||
try expect(@TypeOf(slice) == []i32);
|
try expect(@TypeOf(slice) == []i32);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue