docs: add an Array Slices section (#8429)
parent
5e9b528a9d
commit
10aa03b22f
27
doc/docs.md
27
doc/docs.md
|
@ -586,7 +586,6 @@ println(nums) // "[1, 2, 3]"
|
|||
println(nums[1]) // "2"
|
||||
nums[1] = 5
|
||||
println(nums) // "[1, 5, 3]"
|
||||
println(nums[0..2]) // slicing gives an array "[1, 5]"
|
||||
println(nums.len) // "3"
|
||||
nums = [] // The array is now empty
|
||||
println(nums.len) // "0"
|
||||
|
@ -727,6 +726,32 @@ users.sort(a.age < b.age) // sort by User.age int field
|
|||
users.sort(a.name > b.name) // reverse sort by User.name string field
|
||||
```
|
||||
|
||||
#### Array Slices
|
||||
|
||||
Slices are partial arrays. They represent every element between two indices
|
||||
separated by a .. operator. The right-side index must be greater than or equal
|
||||
to the left side index.
|
||||
|
||||
If a right-side index is absent, it is assumed to be the array length. If a
|
||||
left-side index is absent, it is assumed to be 0.
|
||||
|
||||
```v
|
||||
nums := [1, 2, 3, 4, 5]
|
||||
println(nums[1..4]) // [2, 3, 4]
|
||||
println(nums[..4]) // [1, 2, 3, 4]
|
||||
println(nums[1..]) // [2, 3, 4, 5]
|
||||
```
|
||||
|
||||
All array operations may be performed on slices.
|
||||
Slices can be pushed onto an array of the same type.
|
||||
|
||||
```v
|
||||
array_1 := [3, 5, 4, 7, 6]
|
||||
mut array_2 := [0, 1]
|
||||
array_2 << array_1[..3]
|
||||
println(array_2) // [0, 1, 3, 5, 4]
|
||||
```
|
||||
|
||||
### Maps
|
||||
|
||||
```v
|
||||
|
|
Loading…
Reference in New Issue