Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Latest commit

 

History

History
110 lines (75 loc) · 3.53 KB

5. Data types I.md

File metadata and controls

110 lines (75 loc) · 3.53 KB

Data Types: Arrays, Structs, Constants, Tuples, and Mappings

In this section, we would be going through available data types in Cairo and how they are defined.

Arrays

An Array is a data structure that stores a collection of a single data type.

In Cairo 1.0, they are defined as follows:

use array::ArrayTrait;
fn main() {
    let mut feltArray = ArrayTrait::new();

    feltArray.append(1);
    feltArray.append(2);

    let mut u32Array = ArrayTrait::<u32>::new();

    u32Array.append(1_u32);
    u32Array.append(2_u32);
}
  • First of all, we import ArrayTrait using use array::ArrayTrait. ArrayTrait is a trait(you will be learning more about this in chapter 11 of this guide) that makes the creation and use of arrays possible in Cairo 1.0.
  • Then, let mut feltArray = ArrayTrait::new(); allows us to create the array using the new() method inside of the ArrayTrait. Also, note that the mut makes it mutable. Without the mut keyword, we won't be able to append values to the array.
  • We also created another array u32Array to store u32 values because an array can only take one data types. Here, the data type to be stored in the array is inferred using the first value appended to the array.
  • Apart from new() and append(), there are other methods available for use in the ArrayTrait.

Structs

A Struct is a data structure that serves as a blueprint for the creation of a custom group of related values.

In Cairo 1.0, they are defined as follows:

// Definition
struct Person {
    name: felt252,
    age: u32,
}

//Usage
let person1 = Person{ name: 'Godspower', age: 21 };

//Accessing the values
person1.name;
person1.age;
  • Just like in Rust, structs could also contain logics but we won't treat that now.

Constants

Constants are constants..👀. Just like they're names, these values are constant and immutable values. Here are a few rules about how they are defined in Cairo 1.0.

  • Constants should be defined and assigned in the global scope. That is, they shouldn't be created inside of functions.
  • Types are not inferred for constants. That is, you must set the type as at the time of creation.
  • Constants can't be changed so using the mut keyword is not allowed when creating a constant.
  • Its advisable to use Capital letters for Constant names.

This is how a constants are created:

const NAME: felt252 = 'Godspower Eze';

Tuples

Tuples are similar to arrays but they can store different data types.

They are defined as follows:

#[derive(Drop)]
struct Person {
    name: felt252,
    age: u32
}

let person1 = Person{name: 'Godspower Eze', age: 21_u32};

// Creation
let testTuple = (2_u32, 45, person1);

// Unpacking
let (first, second, third) = testTuple;
  • don't worry about what #[derive(Drop)] does for now, we'd need it for our code to compile, but we'd treat this macro in details in chapter 9.

Mappings

Mappings is a key-value data structure; assigning a value to a key in persistent storage. They are used in Starknet contracts.

They are defined as follows:

#[storage]
struct Storage{
    balances: LegacyMap::<ContractAddress, u32>,
}
  • Mappings are created using the LegacyMap keyword.
  • As previously mentioned, the Storage struct in a Starknet contract houses all storage values.
  • Here, we have a mapping called balances. It maps a contract address(key) to a balance(value).
  • We can update the mapping using self.balances.write(caller, value).