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
This commit is contained in:
David May 2021-07-23 08:32:20 +02:00 committed by GitHub
parent 8ad23d7beb
commit e5b476209a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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