Sequential
Associated Functions for sequential collections
Built-in associated functions for Sequential Collections.
These associated functions are available on all sequential collection types including Vec, arrays, and slice views.
They may be semantically different, but they all share the memory layout:
| capacity | length | elements …. |
Indexing
Sequential collections support index-based element access using the subscript operator [].
Reading
value := sequential_instance[index]
Access an element at the specified index (zero-based).
Example
numbers := [10, 20, 30, 40]
second := numbers[1] // 20
Writing
sequential_instance[index] = value
Assign a new value to the element at the specified index.
Example
mut numbers := [10, 20, 30, 40]
numbers[2] = 99 // [10, 20, 99, 40]
Basic Operations
len
fn len(self) -> Int
Returns the number of elements in the collection.
is_empty
fn is_empty(self) -> Bool
Returns true if the collection contains no elements.
capacity
fn capacity(self) -> Int
Returns the maximum number of elements the collection can hold.
clear
fn clear(mut self)
Sets the length to zero
Element Access
first
fn first(self) -> T
Returns the first element in the collection. Traps if empty.
last
fn last(self) -> T
Returns the last element in the collection. Traps if empty.
Reordering
swap
fn swap(self, index_a: Int, index_b: Int)
Swaps the elements at the two specified indices.
swap_remove
fn swap_remove(mut self, index: Int)
Removes an element by swapping it with the last element and then removing it.
rev
fn rev(self) -> [T]
Returns a reversed view of the collection. The return collection must be a storage.
shuffle_index
fn shuffle_index(mut self, seed: Int, count: Int)
Shuffles indices in the collection (only available for Int slices).
Iteration
for
fn for(self, lambda: (T) -> ())
fn for(self, lambda: (Int, T) -> ())
Iterates over elements, calling the lambda for each element.
while
fn while(self, lambda: (T) -> Bool)
fn while(self, lambda: (Int, T) -> Bool)
Iterates while the lambda returns true.
Filtering & Searching
filter
fn filter(self, lambda: (T) -> Bool) -> [T]
Returns a new slice containing only elements that match the predicate.
filter_mut
fn filter_mut(mut self, lambda: (T) -> Bool)
Removes elements that don’t match the predicate in-place.
find
fn find(self, lambda: (T) -> Bool) -> T?
Returns the first element that matches the predicate.
all
fn all(self, lambda: (T) -> Bool) -> Bool
Returns true if all elements match the predicate.
any
fn any(self, lambda: (T) -> Bool) -> Bool
Returns true if any element matches the predicate.
Index Operations
copy_from_index
fn copy_from_index(self, indices: [Int]) -> [T]
Creates a new slice by copying elements at the specified indices.