<!--
{
  "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/subscript(_:)-9t9gq",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Subscript",
  "symbol" : {
    "kind" : "Instance Subscript",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SryxSicip"
  },
  "title" : "subscript(_:)"
}
-->

# subscript(_:)

Accesses the element at the specified position.

```
subscript(i: Int) -> Element { get nonmutating set }
```

## Parameters

`i`

The position of the element to access. `i` must be in the
range `0..<count`.

## Overview

The following example uses the buffer pointer’s subscript to access and
modify the elements of a mutable buffer pointing to the contiguous
contents of an array:

```
var numbers = [1, 2, 3, 4, 5]
numbers.withUnsafeMutableBufferPointer { buffer in
    for i in stride(from: buffer.startIndex, to: buffer.endIndex - 1, by: 2) {
        let x = buffer[i]
        buffer[i + 1] = buffer[i]
        buffer[i] = x
    }
}
print(numbers)
// Prints "[2, 1, 4, 3, 5]"

Uninitialized memory cannot be initialized to a nontrivial type
using this subscript. Instead, use an initializing method, such as
`initializeElement(at:to:)`
```

> Note: Bounds checks for `i` are performed only in debug mode.

---

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)