Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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.?));
Expand Down