forked from NethermindEth/StarknetByExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dict Cheatsheet (NethermindEth#235)
* simple_vault test implementation * Added dict cheatsheet * Added dict cheatsheet * a little clean up * a little clean up
- Loading branch information
Showing
5 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
listings/getting-started/cairo_cheatsheet/src/dict_example.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ mod struct_example; | |
mod type_casting_example; | ||
mod while_let_example; | ||
mod if_let_example; | ||
mod dict_example; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
``` |