@@ -2093,8 +2093,9 @@ var foo: u8 align(4) = 100;
2093
2093
test "global variable alignment" {
2094
2094
assert(@TypeOf(&foo).alignment == 4);
2095
2095
assert(@TypeOf(&foo) == *align(4) u8);
2096
- const slice = @as(*[1]u8, &foo)[0..];
2097
- assert(@TypeOf(slice) == []align(4) u8);
2096
+ const as_pointer_to_array: *[1]u8 = &foo;
2097
+ const as_slice: []u8 = as_pointer_to_array;
2098
+ assert(@TypeOf(as_slice) == []align(4) u8);
2098
2099
}
2099
2100
2100
2101
fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }
@@ -2187,7 +2188,8 @@ test "basic slices" {
2187
2188
// a slice is that the array's length is part of the type and known at
2188
2189
// compile-time, whereas the slice's length is known at runtime.
2189
2190
// Both can be accessed with the `len` field.
2190
- const slice = array[0..array.len];
2191
+ var known_at_runtime_zero: usize = 0;
2192
+ const slice = array[known_at_runtime_zero..array.len];
2191
2193
assert(&slice[0] == &array[0]);
2192
2194
assert(slice.len == array.len);
2193
2195
@@ -2207,13 +2209,15 @@ test "basic slices" {
2207
2209
{#code_end#}
2208
2210
<p>This is one reason we prefer slices to pointers.</p>
2209
2211
{#code_begin|test|slices#}
2210
- const assert = @import("std").debug.assert;
2211
- const mem = @import("std").mem;
2212
- const fmt = @import("std").fmt;
2212
+ const std = @import("std");
2213
+ const assert = std.debug.assert;
2214
+ const mem = std.mem;
2215
+ const fmt = std.fmt;
2213
2216
2214
2217
test "using slices for strings" {
2215
- // Zig has no concept of strings. String literals are arrays of u8, and
2216
- // in general the string type is []u8 (slice of u8).
2218
+ // Zig has no concept of strings. String literals are const pointers to
2219
+ // arrays of u8, and by convention parameters that are "strings" are
2220
+ // expected to be UTF-8 encoded slices of u8.
2217
2221
// Here we coerce [5]u8 to []const u8
2218
2222
const hello: []const u8 = "hello";
2219
2223
const world: []const u8 = "世界";
@@ -2222,7 +2226,7 @@ test "using slices for strings" {
2222
2226
// You can use slice syntax on an array to convert an array into a slice.
2223
2227
const all_together_slice = all_together[0..];
2224
2228
// String concatenation example.
2225
- const hello_world = try fmt.bufPrint(all_together_slice, "{} {}", .{hello, world});
2229
+ const hello_world = try fmt.bufPrint(all_together_slice, "{} {}", .{ hello, world });
2226
2230
2227
2231
// Generally, you can use UTF-8 and not worry about whether something is a
2228
2232
// string. If you don't need to deal with individual characters, no need
@@ -2239,23 +2243,15 @@ test "slice pointer" {
2239
2243
slice[2] = 3;
2240
2244
assert(slice[2] == 3);
2241
2245
// The slice is mutable because we sliced a mutable pointer.
2242
- assert(@TypeOf(slice) == []u8);
2246
+ // Furthermore, it is actually a pointer to an array, since the start
2247
+ // and end indexes were both comptime-known.
2248
+ assert(@TypeOf(slice) == *[5]u8);
2243
2249
2244
2250
// You can also slice a slice:
2245
2251
const slice2 = slice[2..3];
2246
2252
assert(slice2.len == 1);
2247
2253
assert(slice2[0] == 3);
2248
2254
}
2249
-
2250
- test "slice widening" {
2251
- // Zig supports slice widening and slice narrowing. Cast a slice of u8
2252
- // to a slice of anything else, and Zig will perform the length conversion.
2253
- const array align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13 };
2254
- const slice = mem.bytesAsSlice(u32, array[0..]);
2255
- assert(slice.len == 2);
2256
- assert(slice[0] == 0x12121212);
2257
- assert(slice[1] == 0x13131313);
2258
- }
2259
2255
{#code_end#}
2260
2256
{#see_also|Pointers|for|Arrays#}
2261
2257
0 commit comments