<!--
{
  "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/initialize(fromContentsOf:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sr10initialize14fromContentsOfSiqd___t7ElementQyd__RszSlRd__lF"
  },
  "title" : "initialize(fromContentsOf:)"
}
-->

# initialize(fromContentsOf:)

Initializes the buffer’s memory with every element of the source.

```
func initialize(fromContentsOf source: some Collection<Element>) -> UnsafeMutableBufferPointer<Element>.Index
```

## Parameters

`source`

A collection of elements to be used to
initialize the buffer’s storage.

## Return Value

The index one past the last element of the buffer initialized
by this function.

## Discussion

Prior to calling the `initialize(fromContentsOf:)` method on a buffer, the
memory referenced by the buffer must be uninitialized, or the `Element`
type must be a trivial type. After the call, the memory referenced by the
buffer up to, but not including, the returned index is initialized. The
buffer must reference enough memory to accommodate `source.count`
elements.

The returned index is the position of the next uninitialized element in
the buffer, one past the index of the last element written. If `source`
contains no elements, the returned index is equal to the buffer’s
`startIndex`. If `source` contains as many elements as the buffer can
hold, the returned index is equal to the buffer’s `endIndex`.

> Precondition: `self.count` >= `source.count`

> Note: The memory regions referenced by `source` and this buffer
> must not overlap.

---

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)