<!--
{
  "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/UnsafeMutableBufferPointer/prefix(through:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SlsE6prefix7through11SubSequenceQz5IndexQz_tF::SYNTHESIZED::s:Sr"
  },
  "title" : "prefix(through:)"
}
-->

# prefix(through:)

Returns a subsequence from the start of the collection through the
specified position.

```
func prefix(through position: Self.Index) -> Self.SubSequence
```

## Parameters

`position`

The index of the last element to include in the
resulting subsequence. `position` must be a valid index of the collection
that is not equal to the `endIndex` property.

## Return Value

A subsequence up to, and including, the given position.

## Discussion

The resulting subsequence *includes* the element at the position
specified by the `through` parameter.
The following example searches for the index of the number `40` in an
array of integers, and then prints the prefix of the array up to, and
including, that index:

```
let numbers = [10, 20, 30, 40, 50, 60]
if let i = numbers.firstIndex(of: 40) {
    print(numbers.prefix(through: i))
}
// Prints "[10, 20, 30, 40]"
```

Using the `prefix(through:)` method is equivalent to using a partial
closed range as the collection’s subscript. The subscript notation is
preferred over `prefix(through:)`.

```
if let i = numbers.firstIndex(of: 40) {
    print(numbers[...i])
}
// Prints "[10, 20, 30, 40]"
```

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