0Pricing
Swift Academy · Lesson

One-Sided Ranges

Express open-ended ranges for slicing.

What Are One-Sided Ranges

A one-sided range omits one bound. It is most often used to slice a collection from or up to a position.

let letters = ["a", "b", "c", "d", "e"]
let tail = letters[2...]
print(Array(tail))

From an Index Onward

arr[2...] means "from index 2 to the end". The starting index is included.

let nums = [10, 20, 30, 40]
print(Array(nums[1...]))

All lessons in this course

  1. Closed and Half-Open Ranges
  2. One-Sided Ranges
  3. Counting with stride(from:to:by:)
  4. Ranges as Collections
← Back to Swift Academy