Array<T> structv0.1.0
pub struct Array<T> {
// private fields
}
Move-only: it owns its elements and storage, prohibits copying, and destroys both with ~Array.
Type parameters
| Name | Description |
|---|---|
T | the element type |
Associated functions
Filled
pub func Filled(
allocator: Allocator,
length: uint,
value: T
) -> Array<T>
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.
Terminates the program when the storage cannot be had.
Runs in time proportional to length.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
length | uint | how many elements |
value | T | the value to act on |
Returns: an array of length copies of value
FromSlice
pub func FromSlice(
allocator: Allocator,
items: T[..]
) -> Array<T>
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. Terminates the program when the storage cannot be had.
Runs in time proportional to the slice's length.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
items | T[..] | the elements to take |
Returns: an array holding a copy of those elements
Methods
Clone
pub func Clone(&self) -> Array<T>
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.
Elements are copied with =, so the element type must be plain data that Core::Copyable describes; an
owning element type is rejected while compiling, at the copy. Terminates the program when the storage cannot
be had.
Runs in time proportional to the length.
Returns: an independent copy
Length
pub func Length(&self) -> uint
How many elements the array holds.
self is borrowed. Fixed for the array's whole life.
Returns: how many elements it holds
IsEmpty
pub func IsEmpty(&self) -> bool
Whether the array holds no elements.
self is borrowed.
Returns: whether it holds nothing
Get
pub func Get(&self, 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.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: a copy of the element, or none when the index is out of range
At
pub func At(&self, index: uint) -> &T?
The element at index, borrowed where it sits, or none when there is no such index.
self stays borrowed while the reference is in use. This is how an element that owns something is read
without being copied.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: a reference to the element, or none when the index is out of range
AtMutable
pub func AtMutable(&var self, index: uint) -> &var T?
The element at index, borrowed to be written where it sits, or none when there is no such index.
self stays borrowed exclusively while the reference is in use.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: a writable reference to the element, or none when the index is out of range
Set
pub func Set(
&var self,
index: uint,
value: T
) throws CollectionError
Replaces the element at index with value, destroying the element that was there.
self is borrowed mutably; value is moved in. The old element is destroyed once the new one is in place,
so an element that owns something is released rather than leaked; Replace hands it back instead. Reports
IndexOutOfRange and changes nothing when there is no such index, and value is then destroyed here.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
value | T | the element to store |
Returns: nothing when the element was written, or IndexOutOfRange
Replace
pub func Replace(
&var self,
index: uint,
value: T
) -> T throws CollectionError
Replaces the element at index with value and hands back the element that was there.
self is borrowed mutably; value is moved in and the old element is moved out to the caller rather than
destroyed. Reports IndexOutOfRange and changes nothing when there is no such index, and value is then
destroyed here.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
value | T | the element to store |
Returns: the element that was at index, or IndexOutOfRange
AsSlice
pub func AsSlice(&self) -> 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
AsMutableSlice
pub func AsMutableSlice(&var self) -> 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
Iterate
pub func Iterate(&self) -> 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
References
pub func References(&self) -> 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
MutableReferences
pub func MutableReferences(&var self) -> 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
Destructor
func ~Array(&var self)
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.
Related
WriteArrayDebug<T>
pub func WriteArrayDebug<T: Debug>(
writer: &var TextWriter,
value: &Array<T>,
spec: FormatSpec
) throws 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.
Type parameters
| Name | Description |
|---|---|
T: Debug | the element type, which must know how to show itself |
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
value | &Array<T> | the array to show, borrowed for the call |
spec | FormatSpec | what to show each element under |
Returns: success when the whole rendering was accepted