Day 6 of RareSkills Solana Course.
- Configure Solana to run on localhost:
solana config set --url localhost
- Run the test validator node on another terminal:
solana-test-validator
- Run Solana logs on another terminal:
solana logs
- Build Anchor program:
anchor build
- Sync program_id with Anchor key:
anchor keys sync
- Run tests:
anchor test --skip-local-validator
- If-Else statements
if age >= 18 {
msg!("You are 18 years old or above");
} else {
msg!("You are below 18 years old");
}
parenthesis
are optional for if statements
- Ternary operator
a = age % 2 == 0 ? true : false;
- Match
match age {
1 => {
// Code block executed if age equals 1
msg!("The age is 1");
},
2 | 3 => {
// Code block executed if age equals 2 or 3
msg!("The age is either 2 or 3");
},
4..=6 => {
// Code block executed if age is in the
// range 4 to 6 (inclusive)
msg!("The age is between 4 and 6");
},
_ => {
// Code block executed for any other age
msg!("The age is something else");
}
}
- Default step
for i in 0..10 {
// do something...
}
- Custom step
for i in (0..10).step_by(2) {
// do something...
}
-
Rust only has built-in support for
fixed arrays
. -
For dynamic-length lists, use
vectors
. -
Fixed array
// Declare an array of u32 with a fixed size of 5
let my_array: [u32; 5] = [10, 20, 30, 40, 50];
// Accessing elements of the array
let first_element = my_array[0];
let third_element = my_array[2];
// Declare a mutable array of u32 with a fixed size of 3
let mut mutable_array: [u32; 3] = [100, 200, 300];
// Change the second element from 200 to 250
mutable_array[1] = 250;
- Dynamic array
// Declare a dynamic array-like structure using Vec
let mut dynamic_array: Vec<u32> = Vec::new();
// Add elements to the dynamic array
dynamic_array.push(10);
dynamic_array.push(20);
dynamic_array.push(30);
// Accessing elements of the dynamic array
let first_element = dynamic_array[0];
let third_element = dynamic_array[2];
use std::collections::HashMap;
// Initialize the mapping
let mut my_map = HashMap::new();
// Add a key-value pair to the mapping
my_map.insert(key.to_string(), value.to_string());
// Log the value corresponding to a key from the mapping
msg!("My name is {}", my_map[&key]);
// Defining a struct in Solana
struct Person {
my_name: String,
my_age: u64,
}
// Creating an instance of the struct
let mut person1: Person = Person {
my_name: name,
my_age: age,
};
msg!("{} is {} years old", person1.my_name, person1.my_age);
// Accessing and modifying struct fields
person1.my_name = "Bob".to_string();
person1.my_age = 18;
msg!("{} is {} years old", person1.my_name, person1.my_age);
const MEANING_OF_LIFE_AND_EXISTENCE: u64 = 42;
let mut dynamic_array: Vec<u32> = Vec::from([1,2,3,4,5,6]);
let len = dynamic_array.len(); // this has type usize
let another_var: u64 = 5; // this has type u64
let len_plus_another_var = len as u64 + another_var;
msg!("The result is {}", len_plus_another_var);
- Rust doesn't have
try catch
. - Failures are expected to return
errors
, orpanic
for non-recoverable errors.