---
title: "init(capacity:initializingWith:)"
framework: swift
role: symbol
role_heading: Initializer
path: "swift/contiguousarray/init(capacity:initializingwith:)"
---

# init(capacity:initializingWith:)

Creates an array with the specified capacity, and then calls the given closure with an output span covering the array’s uninitialized memory.

## Declaration

```swift
init<E>(capacity: Int, initializingWith initializer: @_lifetime(span: copy span) (inout OutputSpan<Element>) throws(E) -> Void) throws(E) where E : Error
```

## Parameters

- `capacity`: The number of elements to allocate space for in the new array.
- `initializer`: A closure that initializes the elements of the new array. Parameters: span: An OutputSpan covering uninitialized memory with space for the specified number of elements.

## Discussion

Discussion Inside the closure, initialize elements by appending to the OutputSpan. The OutputSpan keeps track of memory’s initialization state, ensuring safety. Its count at the end of the closure will become the count of the newly-initialized array. note: While the resulting array may have a capacity larger than the requested amount, the OutputSpan passed to the closure will cover exactly the number of elements requested.
