Vec<T>
Vec is a sequential and indexable growable collection type. Values are stored in the environment and are available to contract through the functions defined on Vec. Values stored in the Vec are transmitted to the environment asVals, and when retrieved from the Vec are transmitted back and converted from Val back into their type.
Type Safety
The values in a Vec are not guaranteed to be of typeT and conversion will fail if they are not. Most functions on Vec have a try_ variation that returns a Result that will be Err if the conversion fails. Functions that are not prefixed with try_ will panic if conversion fails.
Functions with an _unchecked suffix will panic if called with indexes that are out-of-bounds.
Constructor Methods
new
from_array
from_slice
from_iter
Vecs. Use Vec::append to join two Vecs.
Example:
Element Access Methods
get
try_get
get_unchecked
- If the position is out-of-bounds.
- If the value at the position cannot be converted to type T.
try_get_unchecked
set
Front/Back Access Methods
first
try_first
first_unchecked
- If the vec is empty.
- If the value at the first position cannot be converted to type T.
try_first_unchecked
last
try_last
last_unchecked
- If the vec is empty.
- If the value at the last position cannot be converted to type T.
try_last_unchecked
Modification Methods
push_front
pop_front
try_pop_front
pop_front_unchecked
- If the vec is empty.
- If the value at the first position cannot be converted to type T.
try_pop_front_unchecked
push_back
pop_back
try_pop_back
pop_back_unchecked
- If the vec is empty.
- If the value at the last position cannot be converted to type T.
try_pop_back_unchecked
insert
remove
None if out-of-bounds.
remove_unchecked
Collection Operations
append
extend_from_array
extend_from_slice
slice
concat (for Vec<Vec<T>>)
Search Methods
contains
first_index_of
None is returned.
last_index_of
None is returned.
binary_search
Result::Ok is returned containing the index of the item.
If the item is not found, Result::Err is returned containing the index of where the item could be inserted to retain the sorted ordering.
Utility Methods
is_empty
len
shuffle
to_shuffled
Iterator Methods
iter
try_iter
into_try_iter
Conversion Methods
env
as_val
to_val
to_vals
Macro
vec!
Usage Examples
Basic Usage
Iterating Over a Vec
Slicing a Vec
See Also
BytesandBytesNfor storing u8s and binary dataMapfor key-value storageStoragefor persistent storage