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

cgen: fixed array slice in function, add docs (#8481)

This commit is contained in:
BigBlack
2021-02-02 01:11:17 +08:00
committed by GitHub
parent 7813ecbb75
commit e3c2604338
3 changed files with 56 additions and 9 deletions

View File

@ -753,6 +753,33 @@ array_2 << array_1[..3]
println(array_2) // [0, 1, 3, 5, 4]
```
### Fixed Size Arrays
V also supports arrays with fixed size. Unlike ordinary arrays, their
length is fixed, so you can not append elements to them, nor shrink them.
You can only modify their elements in place. Note also, that most methods
are defined to work on ordinary arrays, not on fixed size arrays.
However, access to the elements of fixed size arrays, is more efficient,
they need less memory than ordinary arrays, and unlike ordinary arrays,
their data is on the stack, so you may want to use them as buffers if you
do not want additional heap allocations.
You can convert a fixed size array, to an ordinary array with slicing:
```v
mut fnums := [3]int{} // fnums is now a fixed size array with 3 elements.
fnums[0] = 1
fnums[1] = 10
fnums[2] = 100
println(fnums) // => [1, 10, 100]
println(typeof(fnums).name) // => [3]int
//
anums := fnums[0..fnums.len]
println(anums) // => [1, 10, 100]
println(typeof(anums).name) // => []int
```
Note that slicing will cause the data of the fixed array, to be copied to
the newly created ordinary array.
### Maps
```v