Array<T> structv0.1.0

Source
A fixed-length run of elements on storage this array owns.
pub struct Array<T> {
    // private fields
}

Move-only: it owns its elements and storage, prohibits copying, and destroys both with ~Array.

Type parameters

NameDescription
Tthe element type

Associated functions

Filled

pub func Filled(
    allocator: Allocator,
    length: uint,
    value: T
) -> Array<T> ! CollectionError

An array of length elements, every one a copy of value. allocator is borrowed for the call and kept; value is read by value and copied into every position. A length of zero gives an empty array that owns a zero-sized block and is released like any other. A failure hands back no array at all, so there is nothing half-built to destroy. Runs in time proportional to length.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
lengthuinthow many elements
valueTthe value to act on

Returns: an array of length copies of value

Source

FromSlice

pub func FromSlice(
    allocator: Allocator,
    items: T[..]
) -> Array<T> ! CollectionError

An array holding a copy of every element of items, in order. allocator is borrowed for the call and kept; items is borrowed and read. An empty slice gives an empty array. Runs in time proportional to the slice's length.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
itemsT[..]the elements to take

Returns: an array holding a copy of those elements

Source

Methods

Clone

pub func Clone(self: &Array<T>) -> Array<T> ! CollectionError

An independent array holding a copy of every element of this one. self is borrowed and left alone. The copy has its own storage from the same allocator, so both are released independently. Copying elements is what this does, so an element that owns something ends up owned twice — which is why this is only usable where copying an element means what it says. Runs in time proportional to the length.

Returns: an independent copy

Source

Length

pub func Length(self: &Array<T>) -> uint

How many elements the array holds. self is borrowed. Fixed for the array's whole life.

Returns: how many elements it holds

Source

IsEmpty

pub func IsEmpty(self: &Array<T>) -> bool

Whether the array holds no elements. self is borrowed.

Returns: whether it holds nothing

Source

Get

pub func Get(self: &Array<T>, index: uint) -> T?

The element at index, or none when there is no such index. self is borrowed; the element is returned by copy. Bounds-checked, which is what separates this from indexing the slice directly.

NameTypeDescription
indexuintwhich one, counting from zero

Returns: a copy of the element, or none when the index is out of range

Source

Reference

pub func Reference(self: &Array<T>, index: uint) -> (*T)?

A pointer to the element at index, or none when there is no such index. self is borrowed; the pointer borrows the element where it sits. This is how an element that owns something is read without being copied. The pointer is valid until the array is destroyed or moved.

NameTypeDescription
indexuintwhich one, counting from zero

Returns: the address of the element, or none when the index is out of range

Source

Set

pub func Set(
    self: &var Array<T>,
    index: uint,
    value: T
) -> ! CollectionError

Replaces the element at index with value. self is borrowed mutably; value is read by value. Reports IndexOutOfRange and changes nothing when there is no such index. The old element is overwritten rather than destroyed. For an element that owns something, take it out first.

NameTypeDescription
indexuintwhich one, counting from zero
valueTthe value to act on

Returns: nothing when the element was written, or IndexOutOfRange

Source

AsSlice

pub func AsSlice(self: &Array<T>) -> T[..]

The whole array as a borrowed read-only view. self is borrowed. The view is what every Rux/Algorithms query takes. The view is valid until the array is destroyed or moved.

Returns: the elements, borrowed

Source

AsMutableSlice

pub func AsMutableSlice(self: &var Array<T>) -> var T[..]

The whole array as a borrowed writable view. self is borrowed mutably. The view is what every Rux/Algorithms mutation takes, so sorting an array is Sort<T>(array.AsMutableSlice()). The view is valid until the array is destroyed or moved.

Returns: the elements, borrowed for writing

Source

Iterate

pub func Iterate(self: &Array<T>) -> ValueIterator<T>

An iterator over the elements, by copy. self is borrowed and must outlive the iterator. This is the method a for loop finds.

Returns: a cursor yielding copies of the elements

Source

References

pub func References(self: &Array<T>) -> ReferenceIterator<T>

An iterator over pointers to the elements. self is borrowed and must outlive the iterator. This is how elements that own something are walked.

Returns: a cursor yielding the elements' addresses

Source

MutableReferences

pub func MutableReferences(self: &var Array<T>) -> MutableReferenceIterator<T>

An iterator over writable pointers to the elements, without copying them. The array's storage must outlive the iterator and every yielded pointer. Do not destroy or move the array while any of them is in use, and coordinate aliases that access the same elements.

Returns: a cursor yielding writable element addresses

Source

Destructor

func ~Array(self: &var Array<T>)

Destroys every element and releases the storage. Runs when the array's life ends. Elements go first and the block second, which is the only order that works: an element destroyed after its storage was released would be read from freed memory. An array that was moved away is not destroyed where it used to live, so this happens exactly once.

Source

WriteArrayDebug<T>

pub func WriteArrayDebug<T: Debug>(
    writer: &var TextWriter,
    value: &Array<T>,
    spec: FormatSpec
) -> ! FormatError

Writes a borrowed array as [first, second, third]. An empty array is []. The array and every element are borrowed, so an array of a move-only element type is shown without disturbing one of them. spec describes the elements, exactly as for a vector. Reports whatever the writer or an element's implementation reported, stopping at the first refusal.

Source

Type parameters

NameDescription
T: Debugthe element type, which must know how to show itself

Parameters

NameTypeDescription
writer&var TextWriterthe destination
value&Array<T>the array to show, borrowed for the call
specFormatSpecwhat to show each element under

Returns: success when the whole rendering was accepted