aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManlio Perillo <manlio.perillo@gmail.com>2022-12-13 20:58:52 +0100
committerGitHub <noreply@github.com>2022-12-13 14:58:52 -0500
commit21dafd7a54f1b3233e0e73439daee82aa08d4900 (patch)
tree16a7199a0dd4c832ac907d488054b96639e6945f
parent478544239e001eac5bd4840b3c36bc097b1c49ae (diff)
downloadzig-21dafd7a54f1b3233e0e73439daee82aa08d4900.tar.gz
zig-21dafd7a54f1b3233e0e73439daee82aa08d4900.zip
langref: update comments in the slices.zig doctest (#13819)
In the slices.zig doctest, the code `const all_together_slice = all_together[0..]` is incorrect, since the actual type is a pointer to an array, instead of a slice. Use a runtime-known value to slice the array. In the next "slice pointer" test, clarify that slicing a slice to produce a pointer to an array, requires comptime-known indexes, not just constant indexes.
-rw-r--r--doc/langref.html.in9
1 files changed, 6 insertions, 3 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in
index e9f118bc2a..1625e7f800 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -2975,8 +2975,10 @@ test "using slices for strings" {
const world: []const u8 = "世界";
var all_together: [100]u8 = undefined;
- // You can use slice syntax on an array to convert an array into a slice.
- const all_together_slice = all_together[0..];
+ // You can use slice syntax with at least one runtime-know index on an
+ // array to convert an array into a slice.
+ var start : usize = 0;
+ const all_together_slice = all_together[start..];
// String concatenation example.
const hello_world = try fmt.bufPrint(all_together_slice, "{s} {s}", .{ hello, world });
@@ -3002,7 +3004,8 @@ test "slice pointer" {
// The slice is mutable because we sliced a mutable pointer.
try expect(@TypeOf(slice) == []u8);
- // Again, slicing with constant indexes will produce another pointer to an array:
+ // Again, slicing with comptime-known indexes will produce another pointer
+ // to an array:
const ptr2 = slice[2..3];
try expect(ptr2.len == 1);
try expect(ptr2[0] == 3);