| ... | @@ -5337,16 +5337,15 @@ test "implicit cast to comptime_int" { | ... | @@ -5337,16 +5337,15 @@ test "implicit cast to comptime_int" { |
| 5337 | } | 5337 | } |
| 5338 | {#code_end#} | 5338 | {#code_end#} |
| 5339 | {#header_close#} | 5339 | {#header_close#} |
| 5340 | {#header_open|Type Coercion: Arrays and Pointers#} | 5340 | {#header_open|Type Coercion: Slices, Arrays and Pointers#} |
| 5341 | {#code_begin|test|coerce_arrays_and_ptrs#} | 5341 | {#code_begin|test|coerce__slices_arrays_and_ptrs#} |
| 5342 | const std = @import("std"); | 5342 | const std = @import("std"); |
| 5343 | const expect = std.testing.expect; | 5343 | const expect = std.testing.expect; |
| 5344 | | 5344 | |
| 5345 | // This cast exists primarily so that string literals can be | 5345 | // You can assign constant pointers to arrays to a slice with |
| 5346 | // passed to functions that accept const slices. However | 5346 | // const modifier on the element type. Useful in particular for |
| 5347 | // it is probably going to be removed from the language when | 5347 | // String literals. |
| 5348 | // https://github.com/ziglang/zig/issues/265 is implemented. | 5348 | test "*const [N]T to []const T" { |
| 5349 | test "[N]T to []const T" { | | |
| 5350 | var x1: []const u8 = "hello"; | 5349 | var x1: []const u8 = "hello"; |
| 5351 | var x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; | 5350 | var x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 5352 | try expect(std.mem.eql(u8, x1, x2)); | 5351 | try expect(std.mem.eql(u8, x1, x2)); |
| ... | @@ -5356,7 +5355,7 @@ test "[N]T to []const T" { | ... | @@ -5356,7 +5355,7 @@ test "[N]T to []const T" { |
| 5356 | } | 5355 | } |
| 5357 | | 5356 | |
| 5358 | // Likewise, it works when the destination type is an error union. | 5357 | // Likewise, it works when the destination type is an error union. |
| 5359 | test "[N]T to E![]const T" { | 5358 | test "*const [N]T to E![]const T" { |
| 5360 | var x1: anyerror![]const u8 = "hello"; | 5359 | var x1: anyerror![]const u8 = "hello"; |
| 5361 | var x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; | 5360 | var x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 5362 | try expect(std.mem.eql(u8, try x1, try x2)); | 5361 | try expect(std.mem.eql(u8, try x1, try x2)); |
| ... | @@ -5366,7 +5365,7 @@ test "[N]T to E![]const T" { | ... | @@ -5366,7 +5365,7 @@ test "[N]T to E![]const T" { |
| 5366 | } | 5365 | } |
| 5367 | | 5366 | |
| 5368 | // Likewise, it works when the destination type is an optional. | 5367 | // Likewise, it works when the destination type is an optional. |
| 5369 | test "[N]T to ?[]const T" { | 5368 | test "*const [N]T to ?[]const T" { |
| 5370 | var x1: ?[]const u8 = "hello"; | 5369 | var x1: ?[]const u8 = "hello"; |
| 5371 | var x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; | 5370 | var x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 5372 | try expect(std.mem.eql(u8, x1.?, x2.?)); | 5371 | try expect(std.mem.eql(u8, x1.?, x2.?)); |