| ... | @@ -2929,9 +2929,15 @@ test "basic slices" { | ... | @@ -2929,9 +2929,15 @@ test "basic slices" { |
| 2929 | // Both can be accessed with the `len` field. | 2929 | // Both can be accessed with the `len` field. |
| 2930 | var known_at_runtime_zero: usize = 0; | 2930 | var known_at_runtime_zero: usize = 0; |
| 2931 | const slice = array[known_at_runtime_zero..array.len]; | 2931 | const slice = array[known_at_runtime_zero..array.len]; |
| | 2932 | try expect(@TypeOf(slice) == []i32); |
| 2932 | try expect(&slice[0] == &array[0]); | 2933 | try expect(&slice[0] == &array[0]); |
| 2933 | try expect(slice.len == array.len); | 2934 | try expect(slice.len == array.len); |
| 2934 | | 2935 | |
| | 2936 | // If you slice with comptime-known start and end positions, the result is |
| | 2937 | // a pointer to an array, rather than a slice. |
| | 2938 | const array_ptr = array[0..array.len]; |
| | 2939 | try expect(@TypeOf(array_ptr) == *[array.len]i32); |
| | 2940 | |
| 2935 | // Using the address-of operator on a slice gives a single-item pointer, | 2941 | // Using the address-of operator on a slice gives a single-item pointer, |
| 2936 | // while using the `ptr` field gives a many-item pointer. | 2942 | // while using the `ptr` field gives a many-item pointer. |
| 2937 | try expect(@TypeOf(slice.ptr) == [*]i32); | 2943 | try expect(@TypeOf(slice.ptr) == [*]i32); |
| ... | @@ -2974,22 +2980,26 @@ test "using slices for strings" { | ... | @@ -2974,22 +2980,26 @@ test "using slices for strings" { |
| 2974 | } | 2980 | } |
| 2975 | | 2981 | |
| 2976 | test "slice pointer" { | 2982 | test "slice pointer" { |
| | 2983 | var a: []u8 = undefined; |
| | 2984 | try expect(@TypeOf(a) == []u8); |
| 2977 | var array: [10]u8 = undefined; | 2985 | var array: [10]u8 = undefined; |
| 2978 | const ptr = &array; | 2986 | const ptr = &array; |
| | 2987 | try expect(@TypeOf(ptr) == *[10]u8); |
| 2979 | | 2988 | |
| 2980 | // You can use slicing syntax to convert a pointer into a slice: | 2989 | // A pointer to an array can be sliced just like an array: |
| 2981 | const slice = ptr[0..5]; | 2990 | var start: usize = 0; |
| | 2991 | var end: usize = 5; |
| | 2992 | const slice = ptr[start..end]; |
| 2982 | slice[2] = 3; | 2993 | slice[2] = 3; |
| 2983 | try expect(slice[2] == 3); | 2994 | try expect(slice[2] == 3); |
| 2984 | // The slice is mutable because we sliced a mutable pointer. | 2995 | // The slice is mutable because we sliced a mutable pointer. |
| 2985 | // Furthermore, it is actually a pointer to an array, since the start | 2996 | try expect(@TypeOf(slice) == []u8); |
| 2986 | // and end indexes were both comptime-known. | 2997 | |
| 2987 | try expect(@TypeOf(slice) == *[5]u8); | 2998 | // Again, slicing with constant indexes will produce another pointer to an array: |
| 2988 | | 2999 | const ptr2 = slice[2..3]; |
| 2989 | // You can also slice a slice: | 3000 | try expect(ptr2.len == 1); |
| 2990 | const slice2 = slice[2..3]; | 3001 | try expect(ptr2[0] == 3); |
| 2991 | try expect(slice2.len == 1); | 3002 | try expect(@TypeOf(ptr2) == *[1]u8); |
| 2992 | try expect(slice2[0] == 3); | | |
| 2993 | } | 3003 | } |
| 2994 | {#code_end#} | 3004 | {#code_end#} |
| 2995 | {#see_also|Pointers|for|Arrays#} | 3005 | {#see_also|Pointers|for|Arrays#} |