| ... | @@ -2953,6 +2953,14 @@ test "basic slices" { | ... | @@ -2953,6 +2953,14 @@ test "basic slices" { |
| 2953 | const array_ptr = array[0..array.len]; | 2953 | const array_ptr = array[0..array.len]; |
| 2954 | try expect(@TypeOf(array_ptr) == *[array.len]i32); | 2954 | try expect(@TypeOf(array_ptr) == *[array.len]i32); |
| 2955 | | 2955 | |
| | 2956 | // You can perform a slice-by-length by slicing twice. This allows the compiler |
| | 2957 | // to perform some optimisations like recognising a comptime-known length when |
| | 2958 | // the start position is only known at runtime. |
| | 2959 | var runtime_start: usize = 1; |
| | 2960 | const length = 2; |
| | 2961 | const array_ptr_len = array[runtime_start..][0..length]; |
| | 2962 | try expect(@TypeOf(array_ptr_len) == *[length]i32); |
| | 2963 | |
| 2956 | // Using the address-of operator on a slice gives a single-item pointer, | 2964 | // Using the address-of operator on a slice gives a single-item pointer, |
| 2957 | // while using the `ptr` field gives a many-item pointer. | 2965 | // while using the `ptr` field gives a many-item pointer. |
| 2958 | try expect(@TypeOf(slice.ptr) == [*]i32); | 2966 | try expect(@TypeOf(slice.ptr) == [*]i32); |