Skip to content

Commit

Permalink
Add Dict Cheatsheet (#235)
Browse files Browse the repository at this point in the history
* simple_vault test implementation

* Added dict cheatsheet

* Added dict cheatsheet

* a little clean up

* a little clean up
  • Loading branch information
LamsyA authored Aug 8, 2024
1 parent b442f2d commit d05f78e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,16 @@ dependencies = [
"openzeppelin",
]

[[package]]
name = "simple_storage"
version = "0.1.0"

[[package]]
name = "simple_vault"
version = "0.1.0"
dependencies = [
"erc20",
]

[[package]]
name = "snforge_std"
Expand Down
16 changes: 16 additions & 0 deletions listings/getting-started/cairo_cheatsheet/src/dict_example.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ANCHOR: sheet
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");
}
// ANCHOR_END: sheet


1 change: 1 addition & 0 deletions listings/getting-started/cairo_cheatsheet/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ mod struct_example;
mod type_casting_example;
mod while_let_example;
mod if_let_example;
mod dict_example;
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Summary
- [Tuples](./getting-started/cairo_cheatsheet/tuples.md)
- [Struct](./getting-started/cairo_cheatsheet/struct.md)
- [Type casting](./getting-started/cairo_cheatsheet/type_casting.md)
- [Dict](./getting-started/cairo_cheatsheet/dict.md)

# Components

Expand Down
12 changes: 12 additions & 0 deletions src/getting-started/cairo_cheatsheet/dict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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:

```rust
{{#include ../../../listings/getting-started/cairo_cheatsheet/src/dict_example.cairo:sheet}}
```

0 comments on commit d05f78e

Please sign in to comment.