Skip to content

Commit 2712e43

Browse files
authored
doc: add an example on how to get a cloned slice (#13567)
1 parent d2e8302 commit 2712e43

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

doc/docs.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,15 @@ println(a) // `[2, 2, 2, 13, 2, 3, 4]`
11451145
println(b) // `[2, 3, 13]`
11461146
```
11471147

1148+
You can call .clone() on the slice, if you do want to have an independent copy right away:
1149+
```v
1150+
mut a := [0, 1, 2, 3, 4, 5]
1151+
mut b := a[2..4].clone()
1152+
b[0] = 7 // NB: `b[0]` is NOT referring to `a[2]`, as it would have been, without the .clone()
1153+
println(a) // [0, 1, 2, 3, 4, 5]
1154+
println(b) // [7, 3]
1155+
```
1156+
11481157
### Slices with negative indexes
11491158

11501159
V supports array and string slices with negative indexes.

0 commit comments

Comments
 (0)