authorgravatar for manlio.perillo@gmail.comManlio Perillo <manlio.perillo@gmail.com> 2022-12-13 20:58:52+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-13 14:58:52-05:00
log21dafd7a54f1b3233e0e73439daee82aa08d4900
tree16a7199a0dd4c832ac907d488054b96639e6945f
parent478544239e001eac5bd4840b3c36bc097b1c49ae
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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.

1 files changed, 6 insertions(+), 3 deletions(-)

doc/langref.html.in+6-3
......@@ -2975,8 +2975,10 @@ test "using slices for strings" {
29752975 const world: []const u8 = "世界";
29762976
29772977 var all_together: [100]u8 = undefined;
2978 // You can use slice syntax on an array to convert an array into a slice.
2979 const all_together_slice = all_together[0..];
2978 // You can use slice syntax with at least one runtime-know index on an
2979 // array to convert an array into a slice.
2980 var start : usize = 0;
2981 const all_together_slice = all_together[start..];
29802982 // String concatenation example.
29812983 const hello_world = try fmt.bufPrint(all_together_slice, "{s} {s}", .{ hello, world });
29822984
......@@ -3002,7 +3004,8 @@ test "slice pointer" {
30023004 // The slice is mutable because we sliced a mutable pointer.
30033005 try expect(@TypeOf(slice) == []u8);
30043006
3005 // Again, slicing with constant indexes will produce another pointer to an array:
3007 // Again, slicing with comptime-known indexes will produce another pointer
3008 // to an array:
30063009 const ptr2 = slice[2..3];
30073010 try expect(ptr2.len == 1);
30083011 try expect(ptr2[0] == 3);