commands:
scarb new hello_world
scarb build
scarb cairo-run
scarb cairo-run --available-gas=10000
(when using loops)
- 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
.try_into()
safe type casting, returns Option, which needs to be unwraped.into()
- 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.
- returning values from loops:
break value
-
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 besquashed
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)
-
from
array::ArrayTrait
-
You can only append items to the end of an array using
append
and remove items from the front usingpop_front
, you can not change values in the array -
creating an array:
ArrayTrait::new()
orArrayTrait::<u128>::new()
-
getting elements from array:
array.at(index)
-> returns a snapshot to the element at specified index usingunbox()
operator to extract value stored in a box.array.get(index)
-> returns anOption<Box<@T>>
box type containing a snapshot to the element at the specified index, if element exists returnsSome(Box<@T>)
or if element doesn't exist returnsNone
.
-
Span is a struct that represents a snapshot of an
Array
, used for read-only operations on an array. To create span callarray.span()
method.
Felt252Dict<T>
- keys are restricted to
felt252
- operations:
insert(felt252, T) -> ()
to write values to a dictionaryget(felt252) -> T
to read values from it
- Result enum:
enum Result<T, E> {
Ok: T,
Err: E,
}
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();
}
- Unit tests:
#[cfg(test)]
run withscarb cairo-test
#[cfg(test)]
mod tests {
#[test]
#[available_gas(2000000)]
fn it_works() {
assert(1 == 1, '1 != 1');
}
}
array![1, 2, 3]
const a: felt252 = consteval_int!(2 * 2 * 2);
panic
panic_with_felt242()
- nopanic functions (can be only called from other nopanic functions)
fn function_never_panics() nopanic {}
#[derive(Copy, Drop)]
PartialEq
andPartialOrd
-> enabling==, !=, <, >
operatorsClone
andCopy
-> creates deep copy of the variableDrop
andDestruct
-> for moving variables out of scopeSerde
serialization and deserializationstarknet::Store