<!--
{
  "availability" : [
    "iOS: 12.2.0 -",
    "iPadOS: 12.2.0 -",
    "macCatalyst: 12.2.0 -",
    "macOS: 10.14.4 -",
    "tvOS: 12.2.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 5.2.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/OutputSpan",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s10OutputSpanV"
  },
  "title" : "OutputSpan"
}
-->

# OutputSpan

`OutputSpan` is a reference to a contiguous region of memory that starts
with some number of initialized `Element` instances followed by
uninitialized memory. It provides operations to access the items it stores,
as well as to add new elements and to remove existing ones.

```
@frozen struct OutputSpan<Element> where Element : ~Copyable
```

## Topics

### Initializers

[`init()`](/documentation/Swift/OutputSpan/init())

Create an OutputSpan with zero capacity.

[`init(buffer: UnsafeMutableBufferPointer<Element>, initializedCount: Int)`](/documentation/Swift/OutputSpan/init(buffer:initializedCount:)-3tbg3)

Unsafely create an OutputSpan over partly-initialized memory.

[`init(buffer: borrowing Slice<UnsafeMutableBufferPointer<Element>>, initializedCount: Int)`](/documentation/Swift/OutputSpan/init(buffer:initializedCount:)-vie3)

Unsafely create an OutputSpan over partly-initialized memory.

### Instance Properties

[`let capacity: Int`](/documentation/Swift/OutputSpan/capacity)

The total number of elements that this output span can contain.

[`var count: Int`](/documentation/Swift/OutputSpan/count)

The number of initialized elements in this span.

[`var freeCapacity: Int`](/documentation/Swift/OutputSpan/freeCapacity)

The number of additional elements that can be added to this span.

[`var indices: Range<OutputSpan<Element>.Index>`](/documentation/Swift/OutputSpan/indices)

The range of initialized indices for this `OutputSpan`.

[`var isEmpty: Bool`](/documentation/Swift/OutputSpan/isEmpty)

A Boolean value indicating whether the span is empty.

[`var isFull: Bool`](/documentation/Swift/OutputSpan/isFull)

A Boolean value indicating whether the span is full.

[`var mutableSpan: MutableSpan<Element>`](/documentation/Swift/OutputSpan/mutableSpan)

Exclusively borrow the underlying initialized memory for mutation.

[`var span: Span<Element>`](/documentation/Swift/OutputSpan/span)

Borrow the underlying initialized memory for read-only access.

### Instance Methods

[`func append(consuming Element)`](/documentation/Swift/OutputSpan/append(_:))

Append a single element to this span.

[`func append(repeating: Element, count: Int)`](/documentation/Swift/OutputSpan/append(repeating:count:))

Repeatedly append an element to this span.

[`func finalize(for: Slice<UnsafeMutableBufferPointer<Element>>) -> Int`](/documentation/Swift/OutputSpan/finalize(for:)-5utkq)

Consume the output span and return the number of initialized elements.

[`func finalize(for: UnsafeMutableBufferPointer<Element>) -> Int`](/documentation/Swift/OutputSpan/finalize(for:)-83pw0)

Consume the output span and return the number of initialized elements.

[`func removeAll()`](/documentation/Swift/OutputSpan/removeAll())

Remove all this span’s elements and return its memory
to the uninitialized state.

[`func removeLast() -> Element`](/documentation/Swift/OutputSpan/removeLast())

Remove the last initialized element from this span.

[`func removeLast(Int)`](/documentation/Swift/OutputSpan/removeLast(_:))

Remove the last n elements of this span, returning the memory
they occupy to the uninitialized state.

[`func swapAt(OutputSpan<Element>.Index, OutputSpan<Element>.Index)`](/documentation/Swift/OutputSpan/swapAt(_:_:))

Exchange the elements at the two given indices.

[`func swapAt(unchecked: OutputSpan<Element>.Index, unchecked: OutputSpan<Element>.Index)`](/documentation/Swift/OutputSpan/swapAt(unchecked:unchecked:))

Exchange the elements at the two given indices.

[`func withUnsafeMutableBufferPointer<E, R>((UnsafeMutableBufferPointer<Element>, inout Int) throws(E) -> R) throws(E) -> R`](/documentation/Swift/OutputSpan/withUnsafeMutableBufferPointer(_:))

Call the given closure with the unsafe buffer pointer addressed by this
OutputSpan and a mutable reference to its count of initialized elements.

### Subscripts

[`subscript(OutputSpan<Element>.Index) -> Element`](/documentation/Swift/OutputSpan/subscript(_:))

Accesses the element at the specified index.

[`subscript(unchecked _: OutputSpan<Element>.Index) -> Element`](/documentation/Swift/OutputSpan/subscript(unchecked:))

Accesses the element at the specified index.

### Type Aliases

[`typealias Index`](/documentation/Swift/OutputSpan/Index)

The type that represents an initialized index in an `OutputSpan`.

### Default Implementations

[Iterable Implementations](/documentation/Swift/OutputSpan/Iterable-Implementations)

## Relationships

### Conforms To

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

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

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

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

---

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)