rust 101
Welcome to the uncooked rust series, where i'll teach you about rust, which i myself don't know but i'm learning. This will help me understand the concepts better. This is kinda personal notes that i'm taking down from WBA solana cohort so it won't be much detailed and only a basic understanding of rust to get started with anchor.
What is rust?
Rust is a systems programming language that helps you write faster and more reliable software (the book states it idk, yet to try). Unlike python which is dynamically typed and interpreted, rust is statically typed and compiled.
Rust gives you the ability to control low level details such as memory usage, etc.
Some advantages of rust are:
- memory safety without garbage collection
- concurrency and better performance
Key Concepts:
ownership:
- set of rules for memory management
- if rules are violated the program wont compile
- no need for garbage collector
ownership rules:
- every value in rust has a single owner
- when owner goes out of scope the value is dropped
- this ensures memory is managed safely and prevents leaks.
borrowing and references:
- references allow you to refer some value without taking ownership
- use
&
to refer a value &mut
allows modification of borrowed value and&
for immutable(default)- you cannot have mutable and immutable references to the same value simultaneously.
lifetimes:
- lifetimes ensure that references are valid as long as needed
- compiler uses lifetimes to prevent dangling references
shadowing:
- shadowing is a rust feature that lets you declare new variable with same name as prev variable
- the new variable "shadows" the previous one, meaning that the previous variable becomes inaccessible from that point onward in the current scope
//example snippet fn main() { let x = 5; // x is an integer println!("The value of x is: {}", x); let x = x + 1; // x is shadowed with a new value println!("The value of x is: {}", x); ```
structs:
- structs (structures) are a way to create custom data types that can group together
- struct is one of the main ways to organize data
pub struct vault { owner: Pubkey, auth_bump: u8, vault_bump: u8, score: u8 } ```
Some additional notes on installation and writing a world hello program
installing rust on linux, macOS
on MacOS and linux
type the following on the command line
$ curl https://sh.rustup.rs -sSf | sh
The command downloads a script and starts the installation of the rustup tool, which installs the latest stable version of Rust
If installed correctly, Rust is installed now. Great!
you get this.
If not, please google.
world hello program in rust
fn main() {
println!("world, hello!");
}
I'll write about anchor in next post