| ... | @@ -5246,16 +5246,15 @@ test "implicit cast to comptime_int" { | ... | @@ -5246,16 +5246,15 @@ test "implicit cast to comptime_int" { |
| 5246 | } | 5246 | } |
| 5247 | {#code_end#} | 5247 | {#code_end#} |
| 5248 | {#header_close#} | 5248 | {#header_close#} |
| 5249 | {#header_open|Type Coercion: Arrays and Pointers#} | 5249 | {#header_open|Type Coercion: Slices, Arrays and Pointers#} |
| 5250 | {#code_begin|test|coerce_arrays_and_ptrs#} | 5250 | {#code_begin|test|coerce__slices_arrays_and_ptrs#} |
| 5251 | const std = @import("std"); | 5251 | const std = @import("std"); |
| 5252 | const expect = std.testing.expect; | 5252 | const expect = std.testing.expect; |
| 5253 | | 5253 | |
| 5254 | // This cast exists primarily so that string literals can be | 5254 | // You can assign constant pointers to arrays to a slice with |
| 5255 | // passed to functions that accept const slices. However | 5255 | // const modifier on the element type. Useful in particular for |
| 5256 | // it is probably going to be removed from the language when | 5256 | // String literals. |
| 5257 | // https://github.com/ziglang/zig/issues/265 is implemented. | 5257 | test "*const [N]T to []const T" { |
| 5258 | test "[N]T to []const T" { | | |
| 5259 | var x1: []const u8 = "hello"; | 5258 | var x1: []const u8 = "hello"; |
| 5260 | var x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; | 5259 | var x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 5261 | try expect(std.mem.eql(u8, x1, x2)); | 5260 | try expect(std.mem.eql(u8, x1, x2)); |
| ... | @@ -5265,7 +5264,7 @@ test "[N]T to []const T" { | ... | @@ -5265,7 +5264,7 @@ test "[N]T to []const T" { |
| 5265 | } | 5264 | } |
| 5266 | | 5265 | |
| 5267 | // Likewise, it works when the destination type is an error union. | 5266 | // Likewise, it works when the destination type is an error union. |
| 5268 | test "[N]T to E![]const T" { | 5267 | test "*const [N]T to E![]const T" { |
| 5269 | var x1: anyerror![]const u8 = "hello"; | 5268 | var x1: anyerror![]const u8 = "hello"; |
| 5270 | var x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; | 5269 | var x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 5271 | try expect(std.mem.eql(u8, try x1, try x2)); | 5270 | try expect(std.mem.eql(u8, try x1, try x2)); |
| ... | @@ -5275,7 +5274,7 @@ test "[N]T to E![]const T" { | ... | @@ -5275,7 +5274,7 @@ test "[N]T to E![]const T" { |
| 5275 | } | 5274 | } |
| 5276 | | 5275 | |
| 5277 | // Likewise, it works when the destination type is an optional. | 5276 | // Likewise, it works when the destination type is an optional. |
| 5278 | test "[N]T to ?[]const T" { | 5277 | test "*const [N]T to ?[]const T" { |
| 5279 | var x1: ?[]const u8 = "hello"; | 5278 | var x1: ?[]const u8 = "hello"; |
| 5280 | var x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; | 5279 | var x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 5281 | try expect(std.mem.eql(u8, x1.?, x2.?)); | 5280 | try expect(std.mem.eql(u8, x1.?, x2.?)); |