Git Product home page Git Product logo

learning_cairo's Introduction

Learning Cairo

Scarb:

commands:

  • scarb new hello_world
  • scarb build
  • scarb cairo-run
  • scarb cairo-run --available-gas=10000 (when using loops)

Data Types:

  • Boolean(bool)
  • Never type: a type of an expression that should not be evaluated in the encapsulating block, but should cause a flow control change
  • Unit type: () a type that has only one value
  • Numeric types: felt252 (used to represent a field element) and integers
  • Short String: max length of 31 chars stored in felt252

Type Casting:

  • .try_into() safe type casting, returns Option, which needs to be unwraped
  • .into()

Modules and Creates:

  • A module is a named container for items such as structs, enums, functions, constants, and traits.
  • A crate is a single compilation unit. It has a root directory, and a root module defined at the file lib.cairo under this directory.

Control Flow:

  • returning values from loops: break value

Ownership, References and Snapshots:

  • At any given time, a variable can only have one owner.

  • You can pass a variable by-value, by-snapshot, or by-reference to a function.

  • If you pass-by-value, ownership of the variable is transferred to the function.

  • If you want to keep ownership of the variable and know that your function won’t mutate it, you can pass it as a snapshot with @.

  • If you want to keep ownership of the variable and know that your function will mutate it, you can pass it as a mutable reference with ref.

  • #[derive(Copy, Drop) Drop trait -> value cannot go out of scope unless it has been previously moved.

  • Destruct trait -> allows values to automatically be squashed when they go out of scope (must be used every time dictionary is used inside some struct)

  • Desnap operator: * converts snapshot back into a regular value (opposite of the @ operator)

Arrays:

  • from array::ArrayTrait

  • You can only append items to the end of an array using append and remove items from the front using pop_front, you can not change values in the array

  • creating an array: ArrayTrait::new() or ArrayTrait::<u128>::new()

  • getting elements from array:

    • array.at(index) -> returns a snapshot to the element at specified index using unbox() operator to extract value stored in a box.
    • array.get(index) -> returns an Option<Box<@T>> box type containing a snapshot to the element at the specified index, if element exists returns Some(Box<@T>) or if element doesn't exist returns None.
  • Span is a struct that represents a snapshot of an Array, used for read-only operations on an array. To create span call array.span() method.

Dictionaries:

  • Felt252Dict<T>
  • keys are restricted to felt252
  • operations:
    • insert(felt252, T) -> () to write values to a dictionary
    • get(felt252) -> T to read values from it

Enums:

  • Result enum:
enum Result<T, E> {
    Ok: T,
    Err: E,
}

Structs and methods:

use debug::PrintTrait;
#[derive(Copy, Drop)]
struct Rectangle {
    width: u64,
    height: u64,
}

trait RectangleTrait {
    fn area(self: @Rectangle) -> u64;
}

impl RectangleImpl of RectangleTrait {
    fn area(self: @Rectangle) -> u64 {
        (*self.width) * (*self.height)
    }
}

fn main() {
    let rect1 = Rectangle { width: 30, height: 50, };
    rect1.area().print();
}

Tests:

  • Unit tests: #[cfg(test)] run with scarb cairo-test
#[cfg(test)]
mod tests {
    #[test]
    #[available_gas(2000000)]
    fn it_works() {
        assert(1 == 1, '1 != 1');
    }
}

Macros:

  • array![1, 2, 3]
  • const a: felt252 = consteval_int!(2 * 2 * 2);

Errors:

  • panic
  • panic_with_felt242()
  • nopanic functions (can be only called from other nopanic functions) fn function_never_panics() nopanic {}

Derivable Traits:

#[derive(Copy, Drop)]

  • PartialEq and PartialOrd -> enabling ==, !=, <, > operators
  • Clone and Copy -> creates deep copy of the variable
  • Drop and Destruct -> for moving variables out of scope
  • Serde serialization and deserialization
  • starknet::Store

learning_cairo's People

Contributors

petar1304 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.