<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/UniqueArray/replaceSubrange(_:moving:)-4de3f",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s11UniqueArrayVsRi_zrlE15replaceSubrange_6movingySnySiG_SryxGtF"
  },
  "title" : "replaceSubrange(_:moving:)"
}
-->

# replaceSubrange(_:moving:)

Replaces the specified range of elements by moving the elements of a
fully initialized buffer into their place. On return, the buffer is left
in an uninitialized state.

```
mutating func replaceSubrange(_ subrange: Range<Int>, moving newElements: UnsafeMutableBufferPointer<Element>)
```

## Parameters

`subrange`

The subrange of the array to replace. The bounds of
the range must be valid indices in the array.

`newElements`

A fully initialized buffer whose contents to move into
the array.

## Discussion

This method has the effect of removing the specified range of elements
from the array and inserting the new elements starting at the same
location. The number of new elements need not match the number of elements
being removed.

If the array does not have sufficient capacity to perform the replacement,
then this reallocates the array’s storage to extend its capacity, using a
geometric growth rate.

If you pass a zero-length range as the `subrange` parameter, this method
inserts the elements of `newElements` at `subrange.lowerBound`. Calling
the `insert(copying:at:)` method instead is preferred in this case.

Likewise, if you pass a zero-length buffer as the `newElements`
parameter, this method removes the elements in the given subrange
without replacement. Calling the `removeSubrange(_:)` method instead is
preferred in this case.

> Complexity: O(`self.count` + `newElements.count`)

---

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)