1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

doc: add an example on how to get a cloned slice (#13567)

This commit is contained in:
mahdi ramezaan zaade 2022-02-22 18:52:33 +03:30 committed by GitHub
parent d2e8302d21
commit 2712e43802
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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.