Struct soroban_sdk::Vec
source · pub struct Vec<T> { /* private fields */ }
Expand description
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 as Vals, and when retrieved from the Vec are transmitted back and converted from Val back into their type.
The values in a Vec are not guaranteed to be of type T
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.
There are some cases where this lack of guarantee is important:
-
When storing a Vec that has been provided externally as a contract function argument, be aware there is no guarantee that all items in the Vec will be of type
T
. It may be necessary to validate all values, either before storing, or when loading withtry_
variation functions. -
When accessing and iterating over a Vec that has been provided externally as a contract function input, and the contract needs to be resilient to failure, use the
try_
variation functions.
Functions with an _unchecked
suffix will panic if called with indexes that
are out-of-bounds.
To store u8
s and binary data, use Bytes/BytesN instead.
Vec values can be stored as Storage, or in other types like Vec, Map, etc.
Examples
use soroban_sdk::{vec, Env};
let env = Env::default();
let vec = vec![&env, 0, 1, 2, 3];
assert_eq!(vec.len(), 4);
Implementations§
source§impl<T> Vec<T>
impl<T> Vec<T>
sourcepub fn from_array<const N: usize>(env: &Env, items: [T; N]) -> Vec<T>
pub fn from_array<const N: usize>(env: &Env, items: [T; N]) -> Vec<T>
Create a Vec from the array of items.
sourcepub fn from_slice(env: &Env, items: &[T]) -> Vec<T>where
T: Clone,
pub fn from_slice(env: &Env, items: &[T]) -> Vec<T>where
T: Clone,
Create a Vec from the slice of items.
sourcepub fn get(&self, i: u32) -> Option<T>
pub fn get(&self, i: u32) -> Option<T>
Returns the item at the position or None if out-of-bounds.
Panics
If the value at the position cannot be converted to type T.
sourcepub fn try_get(&self, i: u32) -> Result<Option<T>, T::Error>
pub fn try_get(&self, i: u32) -> Result<Option<T>, T::Error>
Returns the item at the position or None if out-of-bounds.
Errors
If the value at the position cannot be converted to type T.
sourcepub fn get_unchecked(&self, i: u32) -> T
pub fn get_unchecked(&self, i: u32) -> T
Returns the item at the position.
Panics
If the position is out-of-bounds.
If the value at the position cannot be converted to type T.
sourcepub fn try_get_unchecked(&self, i: u32) -> Result<T, T::Error>
pub fn try_get_unchecked(&self, i: u32) -> Result<T, T::Error>
sourcepub fn remove(&mut self, i: u32) -> Option<()>
pub fn remove(&mut self, i: u32) -> Option<()>
Removes the item at the position.
Returns None
if out-of-bounds.
sourcepub fn remove_unchecked(&mut self, i: u32)
pub fn remove_unchecked(&mut self, i: u32)
sourcepub fn push_front(&mut self, x: T)
pub fn push_front(&mut self, x: T)
Adds the item to the front.
Increases the length by one, shifts all items up by one, and puts the item in the first position.
sourcepub fn pop_front(&mut self) -> Option<T>
pub fn pop_front(&mut self) -> Option<T>
Removes and returns the first item or None if empty.
Panics
If the value at the first position cannot be converted to type T.
sourcepub fn try_pop_front(&mut self) -> Result<Option<T>, T::Error>
pub fn try_pop_front(&mut self) -> Result<Option<T>, T::Error>
Removes and returns the first item or None if empty.
Errors
If the value at the first position cannot be converted to type T.
sourcepub fn pop_front_unchecked(&mut self) -> T
pub fn pop_front_unchecked(&mut self) -> T
Removes and returns the first item.
Panics
If the vec is empty.
If the value at the first position cannot be converted to type T.
sourcepub fn try_pop_front_unchecked(&mut self) -> Result<T, T::Error>
pub fn try_pop_front_unchecked(&mut self) -> Result<T, T::Error>
sourcepub fn push_back(&mut self, x: T)
pub fn push_back(&mut self, x: T)
Adds the item to the back.
Increases the length by one and puts the item in the last position.
sourcepub fn pop_back(&mut self) -> Option<T>
pub fn pop_back(&mut self) -> Option<T>
Removes and returns the last item or None if empty.
Panics
If the value at the last position cannot be converted to type T.
sourcepub fn try_pop_back(&mut self) -> Result<Option<T>, T::Error>
pub fn try_pop_back(&mut self) -> Result<Option<T>, T::Error>
Removes and returns the last item or None if empty.
Errors
If the value at the last position cannot be converted to type T.
sourcepub fn pop_back_unchecked(&mut self) -> T
pub fn pop_back_unchecked(&mut self) -> T
Removes and returns the last item.
Panics
If the vec is empty.
If the value at the last position cannot be converted to type T.
sourcepub fn try_pop_back_unchecked(&mut self) -> Result<T, T::Error>
pub fn try_pop_back_unchecked(&mut self) -> Result<T, T::Error>
sourcepub fn first(&self) -> Option<T>
pub fn first(&self) -> Option<T>
Returns the first item or None if empty.
Panics
If the value at the first position cannot be converted to type T.
sourcepub fn try_first(&self) -> Result<Option<T>, T::Error>
pub fn try_first(&self) -> Result<Option<T>, T::Error>
Returns the first item or None if empty.
Errors
If the value at the first position cannot be converted to type T.
sourcepub fn first_unchecked(&self) -> T
pub fn first_unchecked(&self) -> T
Returns the first item.
Panics
If the vec is empty.
If the value at the first position cannot be converted to type T.
sourcepub fn try_first_unchecked(&self) -> Result<T, T::Error>
pub fn try_first_unchecked(&self) -> Result<T, T::Error>
sourcepub fn last(&self) -> Option<T>
pub fn last(&self) -> Option<T>
Returns the last item or None if empty.
Panics
If the value at the last position cannot be converted to type T.
sourcepub fn try_last(&self) -> Result<Option<T>, T::Error>
pub fn try_last(&self) -> Result<Option<T>, T::Error>
Returns the last item or None if empty.
Errors
If the value at the last position cannot be converted to type T.
sourcepub fn last_unchecked(&self) -> T
pub fn last_unchecked(&self) -> T
Returns the last item.
Panics
If the vec is empty.
If the value at the last position cannot be converted to type T.
sourcepub fn try_last_unchecked(&self) -> Result<T, T::Error>
pub fn try_last_unchecked(&self) -> Result<T, T::Error>
sourcepub fn extend_from_array<const N: usize>(&mut self, items: [T; N])
pub fn extend_from_array<const N: usize>(&mut self, items: [T; N])
Extend with the items in the array.
sourcepub fn extend_from_slice(&mut self, items: &[T])where
T: Clone,
pub fn extend_from_slice(&mut self, items: &[T])where
T: Clone,
Extend with the items in the slice.
source§impl<T> Vec<T>
impl<T> Vec<T>
sourcepub fn slice(&self, r: impl RangeBounds<u32>) -> Self
pub fn slice(&self, r: impl RangeBounds<u32>) -> Self
Returns a subset of the bytes as defined by the start and end bounds of the range.
Panics
If the range is out-of-bounds.
sourcepub fn shuffle(&mut self)
pub fn shuffle(&mut self)
Returns copy of the vec shuffled using the NOT-SECURE PRNG.
In tests, must be called from within a running contract.
Warning
The pseudo-random generator used to perform the shuffle is not suitable for security-sensitive work.
sourcepub fn to_shuffled(&self) -> Self
pub fn to_shuffled(&self) -> Self
Returns copy of the vec shuffled using the NOT-SECURE PRNG.
In tests, must be called from within a running contract.
Warning
The pseudo-random generator used to perform the shuffle is not suitable for security-sensitive work.
source§impl<T> Vec<T>
impl<T> Vec<T>
sourcepub fn contains(&self, item: impl Borrow<T>) -> bool
pub fn contains(&self, item: impl Borrow<T>) -> bool
Returns true if the Vec contains the item.
sourcepub fn first_index_of(&self, item: impl Borrow<T>) -> Option<u32>
pub fn first_index_of(&self, item: impl Borrow<T>) -> Option<u32>
Returns the index of the first occurrence of the item.
If the item cannot be found None is returned.
sourcepub fn last_index_of(&self, item: impl Borrow<T>) -> Option<u32>
pub fn last_index_of(&self, item: impl Borrow<T>) -> Option<u32>
Returns the index of the last occurrence of the item.
If the item cannot be found None is returned.
sourcepub fn binary_search(&self, item: impl Borrow<T>) -> Result<u32, u32>
pub fn binary_search(&self, item: impl Borrow<T>) -> Result<u32, u32>
Returns the index of an occurrence of the item in an already sorted Vec, or the index of where the item can be inserted to keep the Vec sorted.
If the item is found, 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.
Trait Implementations§
source§impl<T> IntoIterator for Vec<T>
impl<T> IntoIterator for Vec<T>
source§impl<T> Ord for Vec<T>
impl<T> Ord for Vec<T>
source§impl<T> PartialEq for Vec<T>
impl<T> PartialEq for Vec<T>
source§impl<T> PartialOrd for Vec<T>
impl<T> PartialOrd for Vec<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<T> SorobanArbitrary for Vec<T>where
T: SorobanArbitrary,
Available on crate feature testutils
only.
impl<T> SorobanArbitrary for Vec<T>where
T: SorobanArbitrary,
testutils
only.§type Prototype = ArbitraryVec<<T as SorobanArbitrary>::Prototype>
type Prototype = ArbitraryVec<<T as SorobanArbitrary>::Prototype>
Arbitrary
and can be converted to this
SorobanArbitrary
type.source§impl<T> TryFrom<&Vec<T>> for ScVal
impl<T> TryFrom<&Vec<T>> for ScVal
§type Error = ConversionError
type Error = ConversionError
source§impl<T> TryFrom<&Vec<T>> for ScVec
impl<T> TryFrom<&Vec<T>> for ScVec
§type Error = ConversionError
type Error = ConversionError
source§impl<T> TryFrom<Vec<T>> for ScVal
impl<T> TryFrom<Vec<T>> for ScVal
§type Error = ConversionError
type Error = ConversionError
source§impl<T> TryFrom<Vec<T>> for ScVec
impl<T> TryFrom<Vec<T>> for ScVec
§type Error = ConversionError
type Error = ConversionError
source§impl<T> TryFrom<Vec<T>> for VecM<ScVal>
impl<T> TryFrom<Vec<T>> for VecM<ScVal>
§type Error = ConversionError
type Error = ConversionError
source§impl TryFromVal<Env, ()> for Vec<Val>
impl TryFromVal<Env, ()> for Vec<Val>
type Error = ConversionError
fn try_from_val(env: &Env, _v: &()) -> Result<Self, Self::Error>
source§impl<T0> TryFromVal<Env, (T0,)> for Vec<Val>
impl<T0> TryFromVal<Env, (T0,)> for Vec<Val>
type Error = ConversionError
fn try_from_val(env: &Env, v: &(T0,)) -> Result<Self, Self::Error>
source§impl<T0, T1> TryFromVal<Env, (T0, T1)> for Vec<Val>
impl<T0, T1> TryFromVal<Env, (T0, T1)> for Vec<Val>
type Error = ConversionError
fn try_from_val(env: &Env, v: &(T0, T1)) -> Result<Self, Self::Error>
source§impl<T0, T1, T2> TryFromVal<Env, (T0, T1, T2)> for Vec<Val>
impl<T0, T1, T2> TryFromVal<Env, (T0, T1, T2)> for Vec<Val>
type Error = ConversionError
fn try_from_val(env: &Env, v: &(T0, T1, T2)) -> Result<Self, Self::Error>
source§impl<T0, T1, T2, T3> TryFromVal<Env, (T0, T1, T2, T3)> for Vec<Val>
impl<T0, T1, T2, T3> TryFromVal<Env, (T0, T1, T2, T3)> for Vec<Val>
type Error = ConversionError
fn try_from_val(env: &Env, v: &(T0, T1, T2, T3)) -> Result<Self, Self::Error>
source§impl<T0, T1, T2, T3, T4> TryFromVal<Env, (T0, T1, T2, T3, T4)> for Vec<Val>
impl<T0, T1, T2, T3, T4> TryFromVal<Env, (T0, T1, T2, T3, T4)> for Vec<Val>
type Error = ConversionError
fn try_from_val( env: &Env, v: &(T0, T1, T2, T3, T4) ) -> Result<Self, Self::Error>
source§impl<T0, T1, T2, T3, T4, T5> TryFromVal<Env, (T0, T1, T2, T3, T4, T5)> for Vec<Val>
impl<T0, T1, T2, T3, T4, T5> TryFromVal<Env, (T0, T1, T2, T3, T4, T5)> for Vec<Val>
type Error = ConversionError
fn try_from_val( env: &Env, v: &(T0, T1, T2, T3, T4, T5) ) -> Result<Self, Self::Error>
source§impl<T0, T1, T2, T3, T4, T5, T6> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6)> for Vec<Val>
impl<T0, T1, T2, T3, T4, T5, T6> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6)> for Vec<Val>
type Error = ConversionError
fn try_from_val( env: &Env, v: &(T0, T1, T2, T3, T4, T5, T6) ) -> Result<Self, Self::Error>
source§impl<T0, T1, T2, T3, T4, T5, T6, T7> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7)> for Vec<Val>
impl<T0, T1, T2, T3, T4, T5, T6, T7> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7)> for Vec<Val>
type Error = ConversionError
fn try_from_val( env: &Env, v: &(T0, T1, T2, T3, T4, T5, T6, T7) ) -> Result<Self, Self::Error>
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7, T8)> for Vec<Val>
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7, T8)> for Vec<Val>
type Error = ConversionError
fn try_from_val( env: &Env, v: &(T0, T1, T2, T3, T4, T5, T6, T7, T8) ) -> Result<Self, Self::Error>
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)> for Vec<Val>
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)> for Vec<Val>
type Error = ConversionError
fn try_from_val( env: &Env, v: &(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) ) -> Result<Self, Self::Error>
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)> for Vec<Val>
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)> for Vec<Val>
type Error = ConversionError
fn try_from_val( env: &Env, v: &(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) ) -> Result<Self, Self::Error>
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> for Vec<Val>
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> for Vec<Val>
type Error = ConversionError
fn try_from_val( env: &Env, v: &(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) ) -> Result<Self, Self::Error>
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)> for Vec<Val>where
T0: IntoVal<Env, Val>,
T1: IntoVal<Env, Val>,
T2: IntoVal<Env, Val>,
T3: IntoVal<Env, Val>,
T4: IntoVal<Env, Val>,
T5: IntoVal<Env, Val>,
T6: IntoVal<Env, Val>,
T7: IntoVal<Env, Val>,
T8: IntoVal<Env, Val>,
T9: IntoVal<Env, Val>,
T10: IntoVal<Env, Val>,
T11: IntoVal<Env, Val>,
T12: IntoVal<Env, Val>,
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> TryFromVal<Env, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)> for Vec<Val>where
T0: IntoVal<Env, Val>,
T1: IntoVal<Env, Val>,
T2: IntoVal<Env, Val>,
T3: IntoVal<Env, Val>,
T4: IntoVal<Env, Val>,
T5: IntoVal<Env, Val>,
T6: IntoVal<Env, Val>,
T7: IntoVal<Env, Val>,
T8: IntoVal<Env, Val>,
T9: IntoVal<Env, Val>,
T10: IntoVal<Env, Val>,
T11: IntoVal<Env, Val>,
T12: IntoVal<Env, Val>,
type Error = ConversionError
fn try_from_val( env: &Env, v: &(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) ) -> Result<Self, Self::Error>
source§impl<T> TryFromVal<Env, ScVal> for Vec<T>
impl<T> TryFromVal<Env, ScVal> for Vec<T>
type Error = ConversionError
fn try_from_val(env: &Env, val: &ScVal) -> Result<Self, ConversionError>
source§impl<T> TryFromVal<Env, ScVec> for Vec<T>
impl<T> TryFromVal<Env, ScVec> for Vec<T>
type Error = ConversionError
fn try_from_val(env: &Env, val: &ScVec) -> Result<Self, Self::Error>
source§impl<T> TryFromVal<Env, Val> for Vec<T>
impl<T> TryFromVal<Env, Val> for Vec<T>
type Error = ConversionError
fn try_from_val(env: &Env, val: &Val) -> Result<Self, Self::Error>
source§impl<T> TryFromVal<Env, Vec<T>> for Val
impl<T> TryFromVal<Env, Vec<T>> for Val
type Error = ConversionError
fn try_from_val(_env: &Env, v: &Vec<T>) -> Result<Self, Self::Error>
source§impl<T> TryFromVal<Env, Vec<T>> for Vec<Val>
impl<T> TryFromVal<Env, Vec<T>> for Vec<Val>
type Error = Infallible
fn try_from_val(env: &Env, v: &Vec<T>) -> Result<Self, Self::Error>
source§impl<T> TryFromVal<Env, VecObject> for Vec<T>
impl<T> TryFromVal<Env, VecObject> for Vec<T>
type Error = Infallible
fn try_from_val(env: &Env, obj: &VecObject) -> Result<Self, Self::Error>
impl<T> Eq for Vec<T>
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Vec<T>
impl<T> !Send for Vec<T>
impl<T> !Sync for Vec<T>
impl<T> Unpin for Vec<T>where
T: Unpin,
impl<T> !UnwindSafe for Vec<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
§impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,
impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,
type Error = E
fn compare( &self, a: &(T, U, V, W), b: &(T, U, V, W) ) -> Result<Ordering, <C as Compare<(T, U, V, W)>>::Error>
§impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,
impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,
type Error = E
fn compare( &self, a: &(T, U, V, W, X), b: &(T, U, V, W, X) ) -> Result<Ordering, <C as Compare<(T, U, V, W, X)>>::Error>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.