> Aztec.nr API reference, v6.0.0 (mainnet and testnet). Index: https://docs.aztec.network/aztec-nr-api/v6.0.0/llms.txt

# Struct CapsuleArray

Module: `noir_aztec::capsules`

```rust
pub struct CapsuleArray<T>
{ /* private fields */ }
```

A dynamically sized array backed by PXE's non-volatile database (called capsules). Values are persisted until deleted, so they can be e.g. stored during simulation of a transaction and later retrieved during witness generation. All values are scoped per contract address, so external contracts cannot access them.

## Implementations

```rust
impl<T> CapsuleArray<T>
```

```rust
pub unconstrained fn at(
    contract_address: AztecAddress,
    base_slot: Field,
    scope: AztecAddress,
) -> Self
```

Returns a CapsuleArray scoped to a specific address.

Array elements are stored in contiguous slots following the base slot, so there should be sufficient space between array base slots to accommodate elements. A reasonable strategy is to make the base slot a hash of a unique value.

```rust
pub unconstrained fn len(self) -> u32
```

Returns the number of elements stored in the array.

```rust
pub unconstrained fn push(self, value: T)
where
    T: Serialize
```

Stores a value at the end of the array.

```rust
pub unconstrained fn get(self, index: u32) -> T
where
    T: Deserialize
```

Retrieves the value stored in the array at `index`. Throws if the index is out of bounds.

```rust
pub unconstrained fn remove(self, index: u32)
```

Deletes the value stored in the array at `index`. Throws if the index is out of bounds.

```rust
pub unconstrained fn for_each<Env>(self, f: unconstrained fn[Env](u32, T))
where
    T: Deserialize
```

Calls a function on each element of the array.

The function `f` is called once with each array value and its corresponding index, in order (from the first element to the last).

Structurally mutating the array from inside the callback (e.g. via `push` or `remove`) is **not** supported: it can cause elements to be skipped, visited more than once, or read out of bounds.
