Skip to content

ReadOnlySpan

2 min read

No import is needed.

Read the usage guide for examples and common tasks. This reference lists the public declarations for this part of the library. See Reading API signatures for parameter and result notation.

ReadOnlySpan

ReadOnlySpan<T> - a Span<T> without the writers: no indexer setter, set, fill, or copy_from. Take one when a function borrows a run of elements and promises not to change them. The same ref struct escape rules as Span<T> apply (see docs/language/types/spans.md).

public ref struct ReadOnlySpan<T>

constructor

A view of array[offset .. offset + length). Traps unless that range lies inside array.

public constructor(borrow array: T[], offset: int, length: int)

of

Creates a read-only character view of src without copying its contents.

public static fun of(borrow src: string): ReadOnlySpan<T> where T: SpanCharacter

of

A read-only span over the whole of array.

public static fun of(borrow array: T[]): ReadOnlySpan<T>

length

Number of elements this span covers.

public get length(): int

is_empty

True when this span covers no elements.

public fun is_empty(): bool

this

The element at index (relative to this span). Traps if out of range.

public fun this[index: int]: T

this

Accesses the element at index; an out-of-range index panics.

public fun this[index: int]: char where T: SpanCharacter

slice

Returns a view of elements from start (inclusive) to end (exclusive) that shares this span's storage. Panics unless 0 <= start <= end <= length.

public fun slice(start: int, end: int): ReadOnlySpan<T>

slice

Returns a view of UTF-16 units from start (inclusive) to end (exclusive), including views over a string. Panics unless 0 <= start <= end <= length.

public fun slice(start: int, end: int): ReadOnlySpan<T> where T: SpanCharacter

slice

Returns a view from start to the end of this span. Panics unless 0 <= start <= length.

public fun slice(start: int): ReadOnlySpan<T>

index_of

Index of the first element equal to value (by value equality), or None.

public fun index_of(borrow value: T): Option<int>

index_of

Returns the first matching index, or None when no match exists.

public fun index_of(borrow value: T): Option<int> where T: SpanCharacter

contains

True when some element equals value.

public fun contains(borrow value: T): bool

sequence_equal

True when other has the same length and pairwise-equal elements. Element-wise == even for unmanaged T: a bitwise compare would disagree with == on floating-point NaN and -0.0.

public fun sequence_equal(other: ReadOnlySpan<T>): bool

copy_to

Copies every element into dst starting at index 0. Traps if dst is shorter. Overlap within one array is handled like Span<T>.copy_from.

public fun copy_to(dst: Span<T>): void

copy_to

Array-backed unmanaged elements use one bulk copy; strings widen UTF-16 units.

public fun copy_to(dst: Span<T>): void where T: unmanaged

to_array

Copies every element into a fresh, independently-owned array.

public fun to_array(): T[]

to_array

Copies the elements into a new array.

public fun to_array(): T[]where T: unmanaged