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

doc: document array init syntax

This commit is contained in:
Alexander Medvednikov 2020-07-07 11:55:22 +02:00 committed by GitHub
parent ef02373061
commit 1307cfc97c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -437,6 +437,27 @@ It can also append an entire array.
`val in array` returns true if the array contains `val`. See [`in` operator](#in-operator).
 
During initialization you can specify the capacity of the array (`cap`), its initial length (`len`),
and the default element (`init`).
Setting the capacity improves performance of insertions, as it reduces the amount of reallocations in
dynamic arrays:
```v
numbers := []int{ cap: 1000 }
// Now adding new elements is as efficient as setting them directly
for i in 0 .. 1000 {
numbers << i
// same as
// numbers[i] = i
}
```
`[]int{ len: 5, init: -1 }` will create `[-1, -1, -1, -1, -1]`.
#### Array methods
All arrays can be easily printed with `println(arr)` and converted to a string