Skip to content

Latest commit

 

History

History
299 lines (267 loc) · 5.54 KB

README.md

File metadata and controls

299 lines (267 loc) · 5.54 KB

Exploring of Cairo 1

Here are some simple examples to show you how Cairo 1 works. As I continue to develop Cairo 1, I will add more examples. To run the examples, use the following command:

cargo run --bin cairo-run -- -p ./<path_file> 

Here's an example:

cargo run --bin cairo-run -- -p ./src/var/var_1.cairo 

Note: The Cairo 1 version is still in alpha, so it's very likely that the syntax will change.

The base template is from the people at Extropy. You can see it here.

To make these examples, I based them on the repositories.

Contents

Variables

Let

Declaring a Variable. Once a value is assigned, it cannot be changed. It becomes a constant.

fn main() -> felt {
    let a = 1;
    a
}

Mut

Declaring a Mutable Variable. Otherwise, the value cannot be modified again.

fn main() -> felt {
    let mut b = 2;
    b = 3
    b
}

Ref

Modifying a Variable That is Passed as a Reference.

fn main() -> felt {
    let mut n = 1;
    b(ref n);
    n
}

fn b(ref n: felt){
    n = 1;
}

Constant

Declaring global constant.

const NUM: felt = 15;
fn main() -> felt {
    NUM
}

Bool

fn main() -> bool {
    let a = 25;
    if a == 15 {
        return bool::True(());
    } else {
        return bool::False(());
    }
}

Debugging

Print felt

fn main() -> felt {
    let a = 15;
    debug::print_felt('a: ');
    debug::print_felt(a);
    a
}

Conditional Statements

Equality Operator (==)

fn main() {
    let x = 25;
    if x == 0 {
        debug::print_felt('x is equal to 0');
    } else if x == 1 {
        debug::print_felt('x is equal to 1');
    } else {
        debug::print_felt('x is not equal to 0 or 1');
    }
}

Comparison Operators (< > <= >=)

fn main() {
    let x = 10;
    let y = 20;
    if x < y {
        debug::print_felt('x is less than y');
    }
    if x <= y {
        debug::print_felt('x is less than or equal to y');
    }
    if x > y {
        debug::print_felt('x is greater than y');
    }
    if x >= y {
        debug::print_felt('x is greater than or equal to y');
    }
}

AND Operator

fn main() {
    let x = 25;
    if x != 0 & x >= 25 {
        debug::print_felt('x is not equal to 0');
        debug::print_felt('and');
        debug::print_felt('x is greater than or equal to 25');
    } else {
        debug::print_felt('x is equal to 0');
    }
}

OR Operator

fn main() {
    let x = 0;
    if x != 0 | x >= 25 {
        debug::print_felt('x is not equal to 0');
        debug::print_felt('or');
        debug::print_felt('x is greater than or equal to 25');
    } else {
        debug::print_felt('x is equal to 0');
        debug::print_felt('or');
        debug::print_felt('x is less than 25');
    }
}

Negation Operator (!)

A function can be called in the conditional if

fn main() {
    let age = 15;
    if !is_greater(age) {
        debug::print_felt('is less');
    } else {
        debug::print_felt('is greater');
    } 
}

fn is_greater(age: felt) -> bool {
    age >= 18
}

Arrays

Creating, Appending, and Accessing Elements

use array::ArrayTrait;

fn main() {
    let mut arr = ArrayTrait::new();
    arr.append(10);
    arr.append(11);
    arr.append(12);
    let pos2 = arr.at(2_usize);
    debug::print_felt(pos2);
}

Length (len)

use array::ArrayTrait;

fn main() -> usize {
    let mut arr = ArrayTrait::new();
    arr.append(10);
    arr.append(11);
    arr.append(12);
    arr.len()
}

Pop Front

use array::ArrayTrait;

fn main() {
    let mut arr = ArrayTrait::new();
    arr.append(10);
    arr.append(11);
    arr.append(12);
    
    match arr.pop_front() {
        Option::Some(x) => {
            debug::print_felt(x);    
        },
        Option::None(_) => {
            debug::print_felt('None');
        },
    };
}

Enumerations

enum MyEnumShort {
    a: felt,
    b: felt
}

fn main() -> felt {
    let es0 = MyEnumShort::a(10);
    match_short(es0)
}

fn match_short(e: MyEnumShort) -> felt {
    match e {
        MyEnumShort::a(x) => {
            x
        },
        MyEnumShort::b(x) => {
            x * 2
        },
    }
}

Match Expressions

Match with a non-zero value is not supported.

fn main() -> felt {
    let a = 25;
    match_test(a)
}

fn match_test(a: felt) -> felt {
    match a {
        0 => 15, 
        _ => 0,
    }
}