#82: Partial Ranges in Swift

Swift supports ranges with only one bound, which are especially handy when slicing collections.

let values = [10, 20, 30, 40, 50]
values[..<2]   // [10, 20]
values[2...]   // [30, 40, 50]
values[...2]   // [10, 20, 30]

These are called partial ranges and they make slicing code pleasantly compact.

You will see them often when working with strings and arrays.


I'm also on Twitter and GitHub.

Subscribe via RSS.