<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/AnyCollection/subscript(_:)-vb3o",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Subscript",
  "symbol" : {
    "kind" : "Instance Subscript",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s13AnyCollectionVyAByxGSnys0A5IndexVGcip"
  },
  "title" : "subscript(_:)"
}
-->

# subscript(_:)

Accesses a contiguous subrange of the collection’s elements.

```
subscript(bounds: Range<AnyCollection<Element>.Index>) -> AnyCollection<Element>.SubSequence { get }
```

## Parameters

`bounds`

A range of the collection’s indices. The bounds of
the range must be valid indices of the collection.

## Overview

For example, using a `PartialRangeFrom` range expression with an array
accesses the subrange from the start of the range expression until the
end of the array.

```
let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
let streetsSlice = streets[2..<5]
print(streetsSlice)
// ["Channing", "Douglas", "Evarts"]
```

The accessed slice uses the same indices for the same elements as the
original collection. This example searches `streetsSlice` for one of the
strings in the slice, and then uses that index in the original array.

```
let index = streetsSlice.firstIndex(of: "Evarts")!    // 4
print(streets[index])
// "Evarts"
```

Always use the slice’s `startIndex` property instead of assuming that its
indices start at a particular value. Attempting to access an element by
using an index outside the bounds of the slice may result in a runtime
error, even if that index is valid for the original collection.

```
print(streetsSlice.startIndex)
// 2
print(streetsSlice[2])
// "Channing"

print(streetsSlice[0])
// error: Index out of bounds
```

> Complexity: O(1)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)