Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dict Cheatsheet #235

Merged
merged 6 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ dependencies = [
[[package]]
name = "simple_vault"
version = "0.1.0"
dependencies = [
"erc20",
]

[[package]]
name = "snforge_std"
Expand Down
17 changes: 17 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,17 @@
fn dict() {
// ANCHOR: sheet

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](./ch00/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}}
```
Loading