From 2712e43802c842ba8579da8eca9f2d8c12e2ad65 Mon Sep 17 00:00:00 2001 From: mahdi ramezaan zaade Date: Tue, 22 Feb 2022 18:52:33 +0330 Subject: [PATCH] doc: add an example on how to get a cloned slice (#13567) --- doc/docs.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index 1e7d08b3ff..4939f27000 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -1145,6 +1145,15 @@ println(a) // `[2, 2, 2, 13, 2, 3, 4]` println(b) // `[2, 3, 13]` ``` +You can call .clone() on the slice, if you do want to have an independent copy right away: +```v +mut a := [0, 1, 2, 3, 4, 5] +mut b := a[2..4].clone() +b[0] = 7 // NB: `b[0]` is NOT referring to `a[2]`, as it would have been, without the .clone() +println(a) // [0, 1, 2, 3, 4, 5] +println(b) // [7, 3] +``` + ### Slices with negative indexes V supports array and string slices with negative indexes.