rotate(subrange:toStartAt:)

Rotates the elements within the given subrange so that the element at the specified index becomes the start of the subrange.

Rotate.swift:245
@discardableResult mutating func rotate(subrange: Range<Index>, toStartAt newStart: Index) -> Index

Parameters

subrange

The subrange of this collection to rotate.

newStart

The index of the element that should be at the start of subrange after rotating.

Returns

The new index of the element that was at the start of subrange pre-rotation.

Rotating a collection is equivalent to breaking the collection into two sections at the index newStart, and then swapping those two sections. In this example, the numbers array is rotated so that the element at index 3 (40) is first:

var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let oldStart = numbers.rotate(subrange: 0..<4, toStartAt: 2)
// numbers == [30, 40, 10, 20, 50, 60, 70, 80]
// numbers[oldStart] == 10