Dictionary

A dictionary is a data structure used to store key-value pairs, enabling efficient data retrieval. The keys and values in a Cairo dictionary can be of various types, including Felt252. Dictionaries provide fast access to data, as they allow for quick lookups, insertions, and deletions based on the keys.The core functionality of a Felt252Dict<T> is implemented in the trait Felt252DictTrait, which includes all basic operations. Among them, we can find:

  • insert(felt252, T) -> () to write values to a dictionary instance.
  • get(felt252) -> T to read values from it.

For example:

fn dict() {
    let mut Auctions: Felt252Dict<u64> = Default::default();

    Auctions.insert('Bola', 30);
    Auctions.insert('Maria', 40);

    let bola_balance = Auctions.get('Bola');
    assert!(bola_balance == 30, "Bola balance should be 30");

    let maria_balance = Auctions.get('Maria');
    assert!(maria_balance == 40, "Maria balance should be 40");
}
Last change: 2024-08-08, commit: d05f78e