authorgravatar for david.may@hey.comDavid May <david.may@hey.com> 2021-07-23 08:32:20+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-08-31 13:35:24-07:00
logc9ab6baf0697a03c93387940668341d0d517a918
tree116dd041824e8230c55d567af61342456b2f7bd4
parenta45c0c503383d6312d676d114e4067efcac46791

Docs fix array/pointer/slice type coercion section (#9392)

* removed deprecated coercion: [X]T => [] const T * Fixed tests and added desc for first test * Improved heading

1 files changed, 8 insertions(+), 9 deletions(-)

doc/langref.html.in+8-9
...@@ -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#}
5251const std = @import("std");5251const std = @import("std");
5252const expect = std.testing.expect;5252const expect = std.testing.expect;
52535253
5254// This cast exists primarily so that string literals can be5254// You can assign constant pointers to arrays to a slice with
5255// passed to functions that accept const slices. However5255// const modifier on the element type. Useful in particular for
5256// it is probably going to be removed from the language when5256// String literals.
5257// https://github.com/ziglang/zig/issues/265 is implemented.5257test "*const [N]T to []const T" {
5258test "[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}
52665265
5267// Likewise, it works when the destination type is an error union.5266// Likewise, it works when the destination type is an error union.
5268test "[N]T to E![]const T" {5267test "*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}
52765275
5277// Likewise, it works when the destination type is an optional.5276// Likewise, it works when the destination type is an optional.
5278test "[N]T to ?[]const T" {5277test "*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.?));