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

Latest commit

 

History

History
48 lines (29 loc) · 2.54 KB

3. Felt and other primitive types.md

File metadata and controls

48 lines (29 loc) · 2.54 KB

Felt and Other Primitive Types

Data types are indeed an essential part of programming languages, including Cairo. They are used to define the type of data that a variable can hold, which is important for ensuring the correct manipulation and storage of that data.

Cairo1.0 introduces several new data types in addition to the felt data type. Available data types include:

  • Felt252: is a 252-bit integer within the range 0 ≤ x < P, where P is a prime number. It is used for efficient computation and allows for many arithmetic operations to be implemented using modular arithmetic techniques.

  • Boolean: This data type can hold one of two values, true or false, and is often used for conditional statements and logical operations.

  • Short strings: A short string is a string whose length is at most 31 characters, and therefore can fit into a single field element. These are felts under the hood. They are commonly used in assert error messages.

  • Integers: There are several different sizes available, including u8 (an 8-bit unsigned integer), u16 (a 16-bit unsigned integer), u32 (a 32-bit unsigned integer), u64 (a 64-bit unsigned integer), and u256 (a 256-bit unsigned integer).

You could also create Numerical literals from felt252s, by appending a specific data type to them:

    let num_u256 = 1_u256
  • Constants: Constants are fixed and as such can’t be altered after being set. They evaluate to an integer (field element) at compile time You can create a constant using the const keyword, and its compulsory to specify the constant's type alongside like this:
    const AGE: u8 = 25;
  • Enums: are a special data type consisting of a fixed set of constants that you define. You can create an Enum in Cairo 1.0, like this:
    enum Animals { Goat: felt, Dog: felt, Cat: felt }

Cairo 1.0 similar to Rust, supports pattern matchings on Enums. We'd dive deep into this in Chapter 6

  • Struct: This data type is used to create custom data types in Cairo. You can create a struct using the struct keyword:
    struct Student {
        name: felt252,
        age: u8,
    }

We'd delve deeper into Structs in Chapter 5

In addition to these data types, Cairo also supports arrays, which allow multiple values of the same data type to be stored in a single variable, as well as tuples, which allow multiple values of different data types to be grouped together. We'll look deeper into these in Chapter 5