Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 40 additions & 39 deletions documentation/language/08_cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,46 +111,46 @@ Both `address` and `signature` types do not have option variants.


## 4. Records
Defining a `record`
Defining a `record`:
```leo
record Token {
owner: address,
amount: u64,
}
```

Creating a `record`
Creating a `record`:
```leo
let user: User = User {
owner: aleo1ezamst4pjgj9zfxqq0fwfj8a4cjuqndmasgata3hggzqygggnyfq6kmyd4,
balance: 1000u64,
};
```

Accessing `record` fields
Accessing `record` Fields:
```leo
let user_address: address = user.owner;
let user_balance: u64 = user.balance;
```

## 5. Structs
Defining a `struct`
Defining a `struct`:
```leo
struct Message {
sender: address,
object: u64,
}
```

Creating an instance of a `struct`
Creating an Instance of a `struct`:
```leo
let msg: Message = Message {
sender: aleo1ezamst4pjgj9zfxqq0fwfj8a4cjuqndmasgata3hggzqygggnyfq6kmyd4,
object: 42u64,
};
```

Accessing `struct` fields
Accessing `struct` Fields:
```leo
let sender_address: address = msg.sender;
let object_value: u64 = msg.object;
Expand Down Expand Up @@ -189,24 +189,20 @@ Note that because the `address` and `signature` types do not have option variant


## 6. Arrays
Declaring `arrays`
Declaring `arrays`:
```leo
let arrb: [bool; 2] = [true, false];
let arr: [u8; 4] = [1u8, 2u8, 3u8, 4u8];
let empty: [u8; 0] = [];
```

Accessing elements
Accessing Elements:
```leo
let first: u8 = arr[0]; // Get the first element
let second: u8 = arr[1]; // Get the second element
```

Modifying elements

Leo does not support mutable variables, so arrays are immutable after declaration.

Looping Over Arrays
Looping Over Arrays:
```leo
let numbers: [u32; 3] = [5u32, 10u32, 15u32];

Expand All @@ -218,35 +214,27 @@ for i: u8 in 0u8..3u8 {
```

## 7. Tuples
Declaring tuples
Declaring Tuples:
```leo
// NOTE: Tuples cannot be empty!
let t: (u8, bool, field) = (42u8, true, 100field);
```
**Tuples cannot be empty.**

Accessing tuple elements

Use destructuring
Accessing Tuple Elements:

```leo
// Using de-structuring
let (a, b, c) = t;
```

Index-based access
```leo
//Using index-based accessing
let first: u8 = t.0;
let second: bool = t.1;
let third: field = t.2;

```
## 8. Transitions
```leo
transition mint_public(
public receiver: address,
public amount: u64,
) -> Token { /* Your code here */ }
```

## 9. Functions

## 8. Functions
The rules for functions (in the traditional sense) are as follows:

There are three variants of functions:
Expand Down Expand Up @@ -321,7 +309,11 @@ function subtract(a: u64, b: u64) -> u64 {
❌ Cannot call: another `transition` (unless from another program)

### Async Transition
An `async transition` function **modifies private state** exactly like a regular `transition`, but it also includes an onchain portion that can **modify onchain/public state**. It can call `function` and `inline` functions, but **cannot be called by a function or inline**. An `async transition` **must** return a `Future`.
An `async transition` function **modifies private state** exactly like a regular `transition`, but it also includes an onchain portion that can **modify public (onchain) state**.

It can call `function` and `inline` functions, but **cannot be called by a function or inline**.

An `async transition` **must** return a `Future`.
```leo
mapping balances: address => u64

Expand All @@ -338,7 +330,7 @@ async transition mint(receiver: address, amount: u64) -> Future {

❌ Cannot call: another `transition` (unless from another program)

## 10. Loops
## 9. Loops
```leo
let count: u32 = 0u32;

Expand All @@ -347,7 +339,7 @@ for i: u32 in 0u32..5u32 {
}
```

## 11. Conditionals
## 10. Conditionals
```leo
let a: u8 = 1u8;

Expand All @@ -362,32 +354,41 @@ if a == 1u8 {
a = (a == 1u8) ? a + 1u8 : ((a == 2u8) ? a + 2u8 : a + 3u8); // Ternary format
```

## 12. Onchain Storage
## 11. Onchain Storage
### Mappings
```leo
mapping balances: address => u64;

let contains_bal: bool = Mapping::contains(balances, receiver);
let get_bal: u64 = Mapping::get(balances, receiver);
let get_or_use_bal: u64 = Mapping::get_or_use(balances, receiver, 0u64);
let set_bal: () = Mapping::set(balances, receiver, 100u64);
let remove_bal: () = Mapping::remove(balances, receiver);
// Querying
let contains_bal: bool = balances.contains(receiver);
let get_bal: u64 = balances.get(receiver);
let get_or_use_bal: u64 = balances.get_or_use(receiver, 0u64);

// Modifying
balances.set(receiver, 100u64);
balances.remove(balances, receiver);
```
### Storage Variables
```leo
storage var: u8

// Querying
let unwrap_var: u8 = var.unwrap();
let unwrap_or_var: u8 = var.unwrap_or(0u8);

// Modifying
var = 8u8;
var = none;
```
### Storage Vectors
```leo
storage vec: [u8]

// Querying
let len_vec: u32 = vec.len();
let val: u8 = vec.get(idx)

// Modifying
vec.set(idx, value);
vec.push(value);
vec.pop();
Expand All @@ -396,7 +397,7 @@ vec.clear();
```


## 13. Operators
## 12. Operators
### Standard
```leo
// Arithmetic Operators
Expand Down