🦀 A series of notes on rust language
- Google's rust handbook
- Rust documentations
- Package registry for rust in 📦 crates.io
Rust is
- fast and predictable performance like C and C++ (no garbage collector) as well as access to low-level hardware.
- safe (typed language)
- built-in dependency management
- performant (memory can be optimized)
- can be compiled into WebAssembly (WaSM) -> it is a set of instruction that can be executed inside the browser just like JS.
📖 Official installation docs and installation for macOS using homebrew.
brew install rustup-init
rustup-init --profile minimal --default-toolchain nightly --no-modify-path
- Rust analyzer -
rust-lang.rust-analyzer
- WebAssembly -
dtsvet.vscode-wasm
rust analyzer failed to discover workspace in vscode
You need the Config.toml
file for vscode extension to work properly with rust. More info here
shows the rust compiler installed
rustc --version
rustup show # shows all the installed versions
installing a version
rustup install nightly-2021-11-29
rustup default nightly-2021-11-29
💡
Cargo
is the Rust package manager equivalent tonpm
for Javascript.
You could create a new rust project with cargo
:
cargo new project-name
# or you can init the file
mkdir project-name && cd project-name
cargo init
since rust is a compiled language we need to compile it binary code so it can be executed,
rustc hello-world/src/main.rs
./main # execute the binary
# to see the hex dump of the file
# see the compiled version with sequence of the bytes
xxd -g1 main
to run the file using cargo
;
cd project-name
cargo run
💡 Commonly used conventions in
rust
- Naming -> snake case.
- Basic types -> Scalar types doc
- Moving -> happens with dynamic value of the heap
- Copy -> happens with static values
- The
match
keyword is used for pattern matching for enum. - The
struct
keyword is like class andtrait
is similar to interfaces. - more on this - The
impl
makes the compiler determine type at the compile time - The
mod
can be used to expose new module. - Use
cargo fmt
to format rust code. rustfmt - The
cater
is used for absolute import whilesuper
is used to access outside of the current module. This is something similar to../
🎥 Video on Visualizing the rust memory
- Sequence of the bytes are loaded into the virtual memory where the code is saved so the the instruction set can be read
- Stack -> grows high address to low address (much faster than heap)
- Main function runs in the stack
- Heap -> grows low address to high address (bigger size than stack)
- It will allocate a stack frame for main function execution in advance.
2023 © Murshid Azher.