Arbitrum Stylus logo

Stylus by Example

Mapping

Mappings in Solidity are hash tables that store data as key-value pairs, where the key can be any of the built-in data types supported by Ethereum.

Here we will give you the example of mapping in Stylus Rust SDK.

Maps are created with the syntax StorageMap<keyType, StorageType> (rust syntax) or mapping(keyType => valueType) (solidity syntax).

If you define with rust syntax, the keyType should be the type from alloy_primitives, valueType can be be any type from StorageType.

If you define with solidity syntax, the keyType can be any built-in value type, bytes, string, or any contract, valueType can be any type including another mapping or an array.

Mappings are not iterable.

Example

src/lib.rs

1Loading...
1Loading...

Cargo.toml

1Loading...
1Loading...

src/main.rs

1Loading...
1Loading...