| ... | @@ -2990,15 +2990,17 @@ pub fn main() anyerror!void { | ... | @@ -2990,15 +2990,17 @@ pub fn main() anyerror!void { |
| 2990 | {#header_close#} | 2990 | {#header_close#} |
| 2991 | | 2991 | |
| 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#} |
| 2994 | const expect = @import("std").testing.expect; | 3000 | const expect = @import("std").testing.expect; |
| 2995 | | 3001 | |
| 2996 | test "basic slices" { | 3002 | test "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); |