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

# RawSpan

`RawSpan` represents a contiguous region of memory
which contains initialized bytes.

```
@frozen struct RawSpan
```

## Overview

A `RawSpan` instance is a non-owning, non-escaping view into memory.
When a `RawSpan` is created, it inherits the lifetime of the container
owning the contiguous memory, ensuring temporal safety and avoiding
use-after-free errors. Operations on `RawSpan` are bounds-checked,
ensuring spatial safety and avoiding buffer overflow errors.

## Topics

### Initializers

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

Create an empty span.

[`init<Element>(elements: Span<Element>)`](/documentation/Swift/RawSpan/init(elements:))

View a typed span as a raw span.

[`init<Element>(unsafeElements: Span<Element>)`](/documentation/Swift/RawSpan/init(unsafeElements:))

Unsafely view a typed span as a raw span.

### Instance Properties

[`var byteCount: Int`](/documentation/Swift/RawSpan/byteCount)

The number of bytes in the span.

[`var byteOffsets: Range<Int>`](/documentation/Swift/RawSpan/byteOffsets)

The indices that are valid for subscripting the span, in ascending
order.

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

A Boolean value indicating whether the span is empty.

### Instance Methods

[`func byteOffsets(of: borrowing RawSpan) -> Range<Int>?`](/documentation/Swift/RawSpan/byteOffsets(of:))

Returns the byte offsets within this span where the memory represented
by other is located, or nil if other is not located within this span.

[`func extracting(Range<Int>) -> RawSpan`](/documentation/Swift/RawSpan/extracting(_:)-2imhy)

Constructs a new span over the bytes within the supplied range of
positions within this span.

[`func extracting(UnboundedRange) -> RawSpan`](/documentation/Swift/RawSpan/extracting(_:)-3elv4)

Constructs a new span over all the bytes of this span.

[`func extracting(some RangeExpression<Int>) -> RawSpan`](/documentation/Swift/RawSpan/extracting(_:)-8oy0e)

Constructs a new span over the bytes within the supplied range of
positions within this span.

[`func extracting(droppingFirst: Int) -> RawSpan`](/documentation/Swift/RawSpan/extracting(droppingFirst:))

Returns a span over all but the given number of initial bytes.

[`func extracting(droppingLast: Int) -> RawSpan`](/documentation/Swift/RawSpan/extracting(droppingLast:))

Returns a span over all but the given number of trailing bytes.

[`func extracting(first: Int) -> RawSpan`](/documentation/Swift/RawSpan/extracting(first:))

Returns a span containing the initial bytes of this span,
up to the specified maximum length.

[`func extracting(last: Int) -> RawSpan`](/documentation/Swift/RawSpan/extracting(last:))

Returns a span containing the trailing bytes of the span,
up to the given maximum length.

[`func extracting(unchecked: ClosedRange<Int>) -> RawSpan`](/documentation/Swift/RawSpan/extracting(unchecked:)-2hjqs)

Constructs a new span over the bytes within the supplied range of
positions within this span.

[`func extracting(unchecked: Range<Int>) -> RawSpan`](/documentation/Swift/RawSpan/extracting(unchecked:)-527ri)

Constructs a new span over the bytes within the supplied range of
positions within this span.

[`func isIdentical(to: RawSpan) -> Bool`](/documentation/Swift/RawSpan/isIdentical(to:))

Returns a Boolean value indicating whether two instances refer to the same
memory region.

[`func isTriviallyIdentical(to: RawSpan) -> Bool`](/documentation/Swift/RawSpan/isTriviallyIdentical(to:))

Returns a Boolean value indicating whether two instances refer to the same
memory region.

[`func load<T>(fromByteOffset: Int, as: T.Type) -> T`](/documentation/Swift/RawSpan/load(fromByteOffset:as:))

Returns a value constructed from the raw memory at the specified offset.

[`func load<T>(fromByteOffset: Int, as: T.Type, ByteOrder) -> T`](/documentation/Swift/RawSpan/load(fromByteOffset:as:_:))

Returns a value constructed from the raw memory at the specified offset.

[`func unsafeLoad<T>(fromByteOffset: Int, as: T.Type) -> T`](/documentation/Swift/RawSpan/unsafeLoad(fromByteOffset:as:))

Returns a new instance of the given type, constructed from the raw memory
at the specified offset.

[`func unsafeLoad<T>(fromUncheckedByteOffset: Int, as: T.Type) -> T`](/documentation/Swift/RawSpan/unsafeLoad(fromUncheckedByteOffset:as:))

Returns a new instance of the given type, constructed from the raw memory
at the specified offset.

[`func unsafeLoadUnaligned<T>(fromByteOffset: Int, as: T.Type) -> T`](/documentation/Swift/RawSpan/unsafeLoadUnaligned(fromByteOffset:as:))

Returns a new instance of the given type, constructed from the raw memory
at the specified offset.

[`func unsafeLoadUnaligned<T>(fromUncheckedByteOffset: Int, as: T.Type) -> T`](/documentation/Swift/RawSpan/unsafeLoadUnaligned(fromUncheckedByteOffset:as:))

Returns a new instance of the given type, constructed from the raw memory
at the specified offset.

[`func withUnsafeBytes<E, Result>((UnsafeRawBufferPointer) throws(E) -> Result) throws(E) -> Result`](/documentation/Swift/RawSpan/withUnsafeBytes(_:))

Calls the given closure with a pointer to the underlying bytes of
the viewed contiguous storage.

### Subscripts

[`subscript(Int) -> UInt8`](/documentation/Swift/RawSpan/subscript(_:))

Accesses the byte at the specified offset in the span.

[`subscript(unchecked _: Int) -> UInt8`](/documentation/Swift/RawSpan/subscript(unchecked:))

Accesses the byte at the specified offset in the span.

### Default Implementations

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

## Relationships

### Conforms To

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

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

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

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

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

---

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)