Cairo provides dictionaries as part of its core libraries represented by the Felt252Dict<T>
type.
The dictionary key type in Cairo is restricted to felt252
, which differs from other languages where it is possible to define the type of the key type.
To create a dictionary, you can use the default
method of the Default
trait, like:
let mut foo: Felt252Dict<u32> = Default::default();
The basic operations that are most often used are:
insert(felt252, T) -> ()
to write values to a dictionary instance andget(felt252) -> T
to read values from it.And here's how you would use these operations:
let mut foo: Felt252Dict<u32> = Default::default();
// Add a value to the dictionariy
foo.insert('bar', 42);
// Here we update the element of 'bar'
foo.insert('bar', 73);
// To retrieve a dictionary value, you can use
baz = foo.get('bar');
If you try to retrieve the value for a key which does not exist in the dictionary, it will return the zero value of the value type.
This can confuse you, especially if the default value of your value type (for example, 0 for an integer), is a valid value.
This also means there is no way to delete data from a dictionary.
Something to take into account when incorporating this structure into your code.
A friend of yours has an old wholesale store called Gross Store.
The name comes from the quantity of the item that the store sell: it's all in gross unit.
Your friend asked you to implement a point of sale (POS) system for his store.
First, you want to build a prototype for it.
In your prototype, your system will only record the quantity.
Your friend gave you a list of measurements to help you:
Unit | Score |
---|---|
quarter_of_a_dozen | 3 |
half_of_a_dozen | 6 |
dozen | 12 |
small_gross | 120 |
gross | 144 |
great_gross | 1728 |
In order to use the measurement, you need to store the measurement in your program.
let units = units();
// Felt252Dictionary with entries like { 'dozen': 12 }
You need to implement a function that create a new (empty) bill for the customer.
let bill = new_bill();
// Empty Felt252Dictionary
To implement this, you'll need to:
false
if the given unit
is not in the units
map.bill
, indexed by the item name, then return true
.unit
.let bill = new_bill();
let units = units();
let ok = add_item(bill, units, 'carrot', 'dozen');
println!(ok);
// Output: true (since dozen is a valid unit)
Note that the returned value is type
bool
.
To implement this, you'll need to:
false
if the given item is not in the bill.
An item is considered to not be in the bill if its quantity is 0.false
if the given unit
is not in the units
map.false
if the new quantity would be less than 0.bill
, so you can return true
.true
.let bill = new_bill();
let units = units();
let ok = remove_item(bill, units, 'carrot', 'dozen');
println!(ok);
// Output: false (because there are no carrots in the bill)
Note that the returned value is type
bool
.
let mut bill: Felt252Dict<u32> = Default::default();
bill.insert('carrot', 12);
bill.insert('grapes', 3);
let qty = get_item(bill, 'carrot');
println!(qty);
// Output: 12
Note that the returned value is type
u32
.
Sign up to Exercism to learn and master Cairo with 68 exercises, and real human mentoring, all for free.