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" {
52465246}
52475247 {#code_end#}
52485248 {#header_close#}
5249 {#header_open|Type Coercion: Arrays and Pointers#}
5250 {#code_begin|test|coerce_arrays_and_ptrs#}
5249 {#header_open|Type Coercion: Slices, Arrays and Pointers#}
5250 {#code_begin|test|coerce__slices_arrays_and_ptrs#}
52515251const std = @import("std");
52525252const expect = std.testing.expect;
52535253
5254// This cast exists primarily so that string literals can be
5255// passed to functions that accept const slices. However
5256// it is probably going to be removed from the language when
5257// https://github.com/ziglang/zig/issues/265 is implemented.
5258test "[N]T to []const T" {
5254// You can assign constant pointers to arrays to a slice with
5255// const modifier on the element type. Useful in particular for
5256// String literals.
5257test "*const [N]T to []const T" {
52595258 var x1: []const u8 = "hello";
52605259 var x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
52615260 try expect(std.mem.eql(u8, x1, x2));
......@@ -5265,7 +5264,7 @@ test "[N]T to []const T" {
52655264}
52665265
52675266// 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" {
52695268 var x1: anyerror![]const u8 = "hello";
52705269 var x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
52715270 try expect(std.mem.eql(u8, try x1, try x2));
......@@ -5275,7 +5274,7 @@ test "[N]T to E![]const T" {
52755274}
52765275
52775276// Likewise, it works when the destination type is an optional.
5278test "[N]T to ?[]const T" {
5277test "*const [N]T to ?[]const T" {
52795278 var x1: ?[]const u8 = "hello";
52805279 var x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
52815280 try expect(std.mem.eql(u8, x1.?, x2.?));