Rust Notes — Module 6: Error Handling 1. The Philosophy Language Error Mechanism Problem C Return codes (-1, NULL, errno) Easy to ignore, no enforcement Go (value, error) tuples Better, but still ignorable Rust Result<T, E> in the type system Impossible to ignore — compiler enforced If a function can fail, its return type says so. You cannot use the success value without handling the error case first. No hidden exceptions, no surprise crashes from ignored error codes. ...
Rust Notes — Module 7
Rust Notes — Module 7: Packages, Crates & Modules 1. How Rust Compilation Works In C, you hand the compiler a list of files. Each file compiles independently, and a linker stitches the object files together. The problem — the compiler has no idea which files depend on which, so you need a Makefile to track what needs recompiling when something changes. Rust takes a different approach. You hand the compiler one root file (main.rs or lib.rs). The compiler follows mod declarations to find every other file that’s part of the crate. Because Rust knows the full dependency graph from the start, it handles incremental recompilation internally — no Makefile needed. ...
Rust Notes — Module 5
Rust Notes — Module 5: Generics, Closures & Iterators 1. Generics Generics let you write code that works for any type, with the compiler generating specialized versions at compile time. Zero runtime overhead — same as C++ templates. Generic Functions // T is a type parameter — placeholder for any concrete type fn largest<T: PartialOrd>(list: &[T]) -> &T { let mut largest = &list[0]; for item in list { if item > largest { largest = item; } } largest } // works for any type that implements PartialOrd let nums = vec![1, 5, 3, 2]; let chars = vec!['a', 'z', 'm']; println!("{}", largest(&nums)); // 5 println!("{}", largest(&chars)); // z Generic Structs struct Stack<T> { elements: Vec<T>, } // impl block also needs <T> impl<T> Stack<T> { fn new() -> Self { Stack { elements: Vec::new() } } fn push(&mut self, item: T) { self.elements.push(item); } fn pop(&mut self) -> Option<T> { self.elements.pop() } fn peek(&self) -> Option<&T> { self.elements.last() } fn is_empty(&self) -> bool { self.elements.is_empty() } } // type parameter inferred from usage let mut int_stack = Stack::new(); int_stack.push(1); int_stack.push(2); // or explicit let mut move_stack: Stack<Move> = Stack::new(); Generic Enums You’ve already used these — Option<T> and Result<T, E> are generic enums: ...
Rust Notes — Module 3
Rust Notes — Module 3: Structs & Enums 1. Structs Structs group related data together into a named type — same concept as C structs, cleaner syntax. struct User { username: String, email: String, age: u32, active: bool, } Creating and Accessing let user = User { username: String::from("sanketh"), email: String::from("sanketh@example.com"), age: 20, active: true, }; println!("{}", user.username); // field access with . Mutability The entire instance must be mut — you cannot mark individual fields as mutable: ...
Rust Notes — Module 4
Rust Notes — Module 4: Traits 1. What is a Trait? A trait defines a set of behaviors (methods) that a type must implement. It is a contract — any type that implements the trait promises to provide those behaviors. trait Greet { fn hello(&self) -> String; } Concept Rust C++ Go Trait trait abstract class / concept interface Implementation impl Trait for Type override virtual method implicit (duck typing) Dispatch static (default) or dynamic virtual table interface table 2. Implementing a Trait struct Human { name: String } struct Robot { id: u32 } impl Greet for Human { fn hello(&self) -> String { format!("Hi, I'm {}!", self.name) } } impl Greet for Robot { fn hello(&self) -> String { format!("BEEP. I AM UNIT {}.", self.id) } } Each type provides its own implementation of the trait methods. A type can implement any number of traits. You can implement traits for types you didn’t define (with some restrictions — see orphan rule below). 3. Default Implementations Traits can provide default method implementations. Types can override them or inherit them for free: ...
Rust Notes — Module 1
Rust Notes — Module 1: The Basics & Ownership 1. Why Rust? Language Memory Management Cost C Manual (malloc/free) Unsafe — use-after-free, double-free, null deref Go Garbage Collector Safe, but runtime overhead (GC pauses) Rust Ownership system (compiler-enforced) Safe + zero runtime cost Rust’s core promise: memory safety at compile time, with no garbage collector. 2. Variables & Mutability Variables are immutable by default in Rust. You must explicitly opt into mutation with mut. let x = 5; // immutable — cannot be reassigned // x = 6; // ❌ compile error let mut y = 5; // mutable y = 6; // ✅ fine Shadowing You can redeclare a variable with let in the same scope — this is called shadowing: ...
Rust Notes — Module 2
Rust Notes — Module 2: Borrowing & References 1. The Problem Ownership Alone Creates If ownership always moves, passing values to functions becomes painful — you lose access to the value after the call: fn print_string(s: String) { println!("{}", s); } // s is dropped here fn main() { let s = String::from("hello"); print_string(s); // println!("{}", s); // ❌ s was moved into print_string, gone! } You’d have to clone everything or return ownership back — both are tedious. Borrowing solves this. ...
Lifecycle of a Process
Virtualization of the CPU - The Process
Virtualizing the CPU — The Process The Core Idea The OS virtualizes the CPU by running one process, stopping it, running another, and so on. Done fast enough, this creates the illusion that many programs run simultaneously on what might be a single CPU core. The abstraction the OS exposes for this is the Process — simply put, a running program. A program is a lifeless set of instructions sitting on disk. A process is that program, brought to life by the OS. ...
Virtualization
Virtualization of CPU What is Virtualization? Virtualization is the OS’s core trick: take a physical resource and transform it into a more general, powerful, and easy-to-use virtual form of itself. Think of it like a hotel. There is one building (physical resource), but every guest gets their own “private” room with its own key, space, and sense of ownership — completely unaware of the others. The hotel management (OS) handles the illusion. ...