<!--
{
  "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/UnsafeMutableRawBufferPointer",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sw"
  },
  "title" : "UnsafeMutableRawBufferPointer"
}
-->

# UnsafeMutableRawBufferPointer

A mutable nonowning collection interface to the bytes in a
region of memory.

```
@frozen struct UnsafeMutableRawBufferPointer
```

## Overview

You can use an `UnsafeMutableRawBufferPointer` instance in low-level operations to eliminate
uniqueness checks and release mode bounds checks. Bounds checks are always
performed in debug mode.

An `UnsafeMutableRawBufferPointer` instance is a view of the raw bytes in a region of memory.
Each byte in memory is viewed as a `UInt8` value independent of the type
of values held in that memory. Reading from and writing to memory through
a raw buffer are untyped operations. Accessing this collection’s bytes
does not bind the underlying memory to `UInt8`.

In addition to its collection interface, an `UnsafeMutableRawBufferPointer`
instance also supports the following methods provided by
`UnsafeMutableRawPointer`, including bounds checks in debug mode:

- `load(fromByteOffset:as:)`
- `loadUnaligned(fromByteOffset:as:)`
- `storeBytes(of:toByteOffset:as:)`
- `copyMemory(from:)`

To access the underlying memory through typed operations, the memory must
be bound to a trivial type.

> Note: A *trivial type* can be copied bit for bit with no indirection
> or reference-counting operations. Generally, native Swift types that do
> not contain strong or weak references or other forms of indirection are
> trivial, as are imported C structs and enums. Copying memory that
> contains values of nontrivial types can only be done safely with a typed
> pointer. Copying bytes directly from nontrivial, in-memory values does
> not produce valid copies and can only be done by calling a C API, such as
> `memmove()`.

# UnsafeMutableRawBufferPointer Semantics

An `UnsafeMutableRawBufferPointer` instance is a view into memory and does not own the memory
that it references. Copying a variable or constant of type `UnsafeMutableRawBufferPointer` does
not copy the underlying memory. However, initializing another collection
with an `UnsafeMutableRawBufferPointer` instance copies bytes out of the referenced memory and
into the new collection.

The following example uses `someBytes`, an `UnsafeMutableRawBufferPointer` instance, to
demonstrate the difference between assigning a buffer pointer and using a
buffer pointer as the source for another collection’s elements. Here, the
assignment to `destBytes` creates a new, nonowning buffer pointer
covering the first `n` bytes of the memory that `someBytes`
references—nothing is copied:

```
var destBytes = someBytes[0..<n]
```

Next, the bytes referenced by `destBytes` are copied into `byteArray`, a
new `[UInt8]` array, and then the remainder of `someBytes` is appended to
`byteArray`:

```
var byteArray: [UInt8] = Array(destBytes)
byteArray += someBytes[n..<someBytes.count]
```

Assigning into a ranged subscript of an `UnsafeMutableRawBufferPointer` instance copies bytes
into the memory. The next `n` bytes of the memory that `someBytes`
references are copied in this code:

```
destBytes[0..<n] = someBytes[n..<(n + n)]
```

## Relationships

### Conforms To

[`ContiguousBytes`](/documentation/Foundation/ContiguousBytes)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`BitwiseCopyable`](/documentation/Swift/BitwiseCopyable)

[`Copyable`](/documentation/Swift/Copyable)

[`BidirectionalCollection`](/documentation/Swift/BidirectionalCollection)

[`Collection`](/documentation/Swift/Collection)

[`ConvertibleToBytes`](/documentation/Swift/ConvertibleToBytes)

[`Escapable`](/documentation/Swift/Escapable)

[`Sequence`](/documentation/Swift/Sequence)

[`AtomicRepresentable`](/documentation/Synchronization/AtomicRepresentable)

[`RandomAccessCollection`](/documentation/Swift/RandomAccessCollection)

[`MutableCollection`](/documentation/Swift/MutableCollection)

---

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)