authorgravatar for david.may@hey.comDavid May <david.may@hey.com> 2021-07-23 08:32:20+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-07-23 09:32:20+03:00
loge5b476209a1215a03164890d331e2013f20882a6
treeb3b57c569f62472c7fea0f783da680bbeb0a9e8b
parent8ad23d7beba9403d83ab961912d62002d9cb9602
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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
...@@ -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#}
5342const std = @import("std");5342const std = @import("std");
5343const expect = std.testing.expect;5343const expect = std.testing.expect;
53445344
5345// This cast exists primarily so that string literals can be5345// You can assign constant pointers to arrays to a slice with
5346// passed to functions that accept const slices. However5346// const modifier on the element type. Useful in particular for
5347// it is probably going to be removed from the language when5347// String literals.
5348// https://github.com/ziglang/zig/issues/265 is implemented.5348test "*const [N]T to []const T" {
5349test "[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}
53575356
5358// Likewise, it works when the destination type is an error union.5357// Likewise, it works when the destination type is an error union.
5359test "[N]T to E![]const T" {5358test "*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}
53675366
5368// Likewise, it works when the destination type is an optional.5367// Likewise, it works when the destination type is an optional.
5369test "[N]T to ?[]const T" {5368test "*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.?));