Provides read-only access to elements in a storage Vec. This trait enables retrieving elements and checking the vector’s length without modifying the underlying storage.

Signature

pub trait VecTrait

Trait functions

get

Returns a storage path to the element at the specified index, or None if out of bounds.

Signature

fn get(self: T, index: u64) -> OptionElementType>>

Examples

use starknet::storage::{Vec, VecTrait, StoragePointerReadAccess};

#[storage]
struct Storage {
    numbers: Vec,
}

fn maybe_number(self: @ContractState, index: u64) -> Option {
    self.numbers.get(index).map(|ptr| ptr.read())
}

at

Returns a storage path to access the element at the specified index.

Panics

Panics if the index is out of bounds.

Examples

use starknet::storage::{Vec, VecTrait, StoragePointerReadAccess};

#[storage]
struct Storage {
    numbers: Vec,
}

fn get_number(self: @ContractState, index: u64) -> u256 {
    self.numbers.at(index).read()
}

Signature

fn at(self: T, index: u64) -> StoragePathElementType>

len

Returns the number of elements in the vector. The length is stored at the vector’s base storage address and is automatically updated when elements are appended.

Signature

fn len(self: T) -> u64

Examples

use starknet::storage::{Vec, VecTrait};

#[storage]
struct Storage {
    numbers: Vec,
}

fn is_empty(self: @ContractState) -> bool {
    self.numbers.len() == 0
}

Trait types

ElementType

Signature

type ElementType;