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

feat: Adding Tuple section to Cairo Cheatsheet #110

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#[starknet::contract]
mod TupleExample {
use starknet::{ContractAddress, get_caller_address};

#[storage]
struct Storage {
user_data: (ContractAddress, u64, bool)
}

#[external(v0)]
#[generate_trait]
impl TupleExampleImpl of ITupleExampleImpl {
fn store_tuple(ref self: ContractState, address: ContractAddress, age: u64, active: bool) {
let user_tuple = (address, age, active);
self.user_data.write(user_tuple);
}

fn read_tuple(self: @ContractState) -> (ContractAddress, u64, bool) {
let stored_tuple = self.user_data.read();
let (address, age, active) = stored_tuple;
(address, age, active)
}
}
}
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Summary
- [Arrays](./ch00/cairo_cheatsheet/arrays.md)
- [Loop](./ch00/cairo_cheatsheet/loop.md)
- [Match](./ch00/cairo_cheatsheet/match.md)
- [Tuples](./ch00/cairo_cheatsheet/tuples.md)
- [Struct](./ch00/cairo_cheatsheet/struct.md)
- [Type casting](./ch00/cairo_cheatsheet/type_casting.md)

Expand Down
8 changes: 8 additions & 0 deletions src/ch00/cairo_cheatsheet/tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Tuples

Tuples is a data type to group a fixed number of items of potentially different types into a single compound structure. Unlike arrays, tuples have a set length and can contain elements of varying types. Once a tuple is created, its size cannot change.
For example:

```rust
{{#include ../../../listings/ch00-getting-started/cairo_cheatsheet/src/tuple_example.cairo}}
```