Rust
Intro to Rust
- Rust is a statically and strongly typed systems programming language
- statically - all types are known at compile time
- before your program runs, the compiler already knows exactly what every variable is (e.g.,
int) and how it behaves - e.g., cpp is static, python is not static (decides variable type at runtime)
- before your program runs, the compiler already knows exactly what every variable is (e.g.,
- strongly - types are designed to make it harder to write incorrect programs
- systems - systems programming is about coding for the software that interacts directly with the bare metal of the hardware (e.g., writing OS, game engines, real-time controllers for physical hardware)
- statically - all types are known at compile time
- β οΈ Key difference btwn rust and C/C++ is that rust is safe by default
- safe by default - languages like C/C++ are not safe by default, because they allow memory corruption if the programmer does not allocate/free memory correctly - rust prevents this by having the compiler act as an ultra-strict proofreader.
- higher-level languages solve this ^ with a "garbage collector," which is a background process that constantly sweeps through memory to clean up unused data
- π Unifying principles behind Rust:
- strictly enforcing safe borrowing of data
- functions, methods, and closures to operate on data
- functions - standalone blocks of code
- methods - functions attached to specific data structures
- closures - anonymous inline functions (e.g. python
lambda)
- tuples, structs, and enums to aggregate data
- pattern matching to select and destructure data
- traits to define behavior on data
Operating system:
- Rust compiler - program that inputs the human-readable Rust code (in a
.rsfile) and translates it into raw machine code (a binary executable) that the computer's CPU can actually understand and run - Package manager for rust: Cargo
Installing Rust
curl https://sh.rustup.rs -sSf | sh
rustup component add rust-docs
curl: command-line tool for making HTTP requests (accessing stuff from browser)-sSf:-s: silent (suppresses progress bars + extra output)-S: show errors (still print errors if something goes wrong ^)-f: makes curl exit with an error if the HTTP request fails (e.g., 404)
|: pipe operator in unix - takes the output of the left command and sends it as an input to the right commandcurl (download script) β sh (run script)
sh: unix shell
Rust basics
https://stevedonovan.github.io/rust-gentle-intro/1-basics.html
Package manager
Cargo:
cargo new project-name
cd project-name
cargo add package-name
cargo build
cargo run