Skip to content

murshidazher/rust-notes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🦀 A series of notes on rust language

Table of Contents

Why Rust ?

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.

Installation

📖 Official installation docs and installation for macOS using homebrew.

brew install rustup-init
rustup-init --profile minimal --default-toolchain nightly --no-modify-path

Visual Studio Code Extensions

  • Rust analyzer - rust-lang.rust-analyzer
  • WebAssembly - dtsvet.vscode-wasm

Common Errors

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

Manage Rust Versions

📖 managing rust versions using rustup

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

Create project

💡 Cargo is the Rust package manager equivalent to npm 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

Conventions & Syntax

💡 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 and trait 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 while super is used to access outside of the current module. This is something similar to ../

How Rust Memory it works ?

🎥 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.

LICENSE

2023 © Murshid Azher.