authorgravatar for 77922942+expikr@users.noreply.github.comexpikr <77922942+expikr@users.noreply.github.com> 2023-10-28 07:11:56+08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-10-27 23:11:56+00:00
logaecdf6ad50fef3ef68d699c2ef72b5904fdd3553
treee149c18e2095896dcb0fe9dc10b446e4afaebdd8
parent1c85b0acbb5148dec7ff671e767354f3df5c6b83
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Doc: elaborate on Slice section

somehow the concept of slices had less elaboration in its own dedicated section than the mentions of it elsewhere.

1 files changed, 6 insertions(+), 4 deletions(-)

doc/langref.html.in+6-4
...@@ -2990,15 +2990,17 @@ pub fn main() anyerror!void {...@@ -2990,15 +2990,17 @@ pub fn main() anyerror!void {
2990 {#header_close#}2990 {#header_close#}
29912991
2992 {#header_open|Slices#}2992 {#header_open|Slices#}
2993 <p>
2994 A slice is a pointer and a length. The difference between an array and
2995 a slice is that the array's length is part of the type and known at
2996 compile-time, whereas the slice's length is known at runtime.
2997 Both can be accessed with the `len` field.
2998 </p>
2993 {#code_begin|test_safety|test_basic_slices|index out of bounds#}2999 {#code_begin|test_safety|test_basic_slices|index out of bounds#}
2994const expect = @import("std").testing.expect;3000const expect = @import("std").testing.expect;
29953001
2996test "basic slices" {3002test "basic slices" {
2997 var array = [_]i32{ 1, 2, 3, 4 };3003 var array = [_]i32{ 1, 2, 3, 4 };
2998 // A slice is a pointer and a length. The difference between an array and
2999 // a slice is that the array's length is part of the type and known at
3000 // compile-time, whereas the slice's length is known at runtime.
3001 // Both can be accessed with the `len` field.
3002 var known_at_runtime_zero: usize = 0;3004 var known_at_runtime_zero: usize = 0;
3003 const slice = array[known_at_runtime_zero..array.len];3005 const slice = array[known_at_runtime_zero..array.len];
3004 try expect(@TypeOf(slice) == []i32);3006 try expect(@TypeOf(slice) == []i32);