0Pricing
Swift Academy · Lesson

One-Sided Ranges

Express open-ended ranges for slicing.

One-Sided Ranges is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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...]))

Up to an Index

arr[..<3] means "from the start up to but not including index 3".

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

Through an Index

arr[...2] means "from the start through index 2", including index 2.

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

Slices Share Indices

A slice keeps the original indices. The first element of arr[2...] is still at index 2, not 0.

let nums = [10, 20, 30, 40]
let slice = nums[2...]
print(slice.startIndex)
print(slice[2])

Converting a Slice

To reindex from 0 or get a standalone array, wrap the slice in Array(...).

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

One-Sided in Pattern Matching

One-sided ranges also work as switch patterns: case 18... matches 18 and above.

let age = 70
switch age {
case ..<18:
    print("Minor")
case 18...:
    print("Adult")
default:
    print("?")
}

Lower One-Sided Pattern

case ..<0 matches everything below zero, useful for catching negatives.

let value = -3
switch value {
case ..<0:
    print("negative")
case 0:
    print("zero")
default:
    print("positive")
}

Prefix of a String

One-sided ranges slice strings too, when used with string indices. Here we drop the first character via a slice.

let word = "Swift"
let chars = Array(word)
let rest = chars[1...]
print(String(rest))

Combining Slices

You can take a head and a tail of an array using two one-sided slices.

let data = [1, 2, 3, 4, 5, 6]
let head = Array(data[..<2])
let tail = Array(data[4...])
print(head)
print(tail)

Safe Slicing

Slicing past the bounds crashes, so guard the index first when it comes from input.

let nums = [1, 2, 3]
let start = 1
if start < nums.count {
    print(Array(nums[start...]))
}

Quick Check

Test one-sided ranges.

Recap: One-Sided Ranges

One-sided ranges like arr[2...], arr[..<3], and arr[...2] slice from or up to a position. Slices keep original indices, so use Array(...) to reindex. They also work as switch patterns.

let a = [1, 2, 3, 4]
print(Array(a[2...]))

Frequently asked questions

Is the “One-Sided Ranges” lesson free?

Yes — the full text of “One-Sided Ranges” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “One-Sided Ranges”?

Express open-ended ranges for slicing. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “One-Sided Ranges” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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