Array Slicing
You can use the Range type in subscripts to get a segment of elements at once, resulting in a new array. The Range syntax is start..end, meaning from start to end (not including end).
You can omit the start or end position of the Range in the subscript. When the start is omitted, it begins from index 0; when the end is omitted, it continues to the end of the array. Slicing operations return a new array, and modifying the slice does not affect the original array.
When iterating by index, you can use for (i in 0..arr.size) with index access, or use for ((i, v) in arr.enumerate()) to get both the index and value simultaneously.
Note
In the slice range
start..end, neither start nor end can exceed the array length. If start is greater than end or negative numbers appear, a runtime error will be triggered.Run the code to observe slicing operations. Try creating a slice and modifying it to see if the original array changes.