From 10aa03b22fcbbc89a51d2667fd8980b50105c720 Mon Sep 17 00:00:00 2001 From: William Gooch Date: Sat, 30 Jan 2021 05:49:06 -0500 Subject: [PATCH] docs: add an Array Slices section (#8429) --- doc/docs.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/doc/docs.md b/doc/docs.md index 5ddca07769..41c7a955f2 100644 --- a/doc/docs.md +++ b/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