Getting Rusty
I started this MD book to track my learning of Rust Programming while aiming to cover the full spectrum of Rust.
The standard process of learning rust can be through any of the following
- The official Rust Programming Book
- Interactive Rustling Course
- Rust by Example
- Comprehensive Rust - Google
Many examples in this mdbook are copied/adopted from these resources.
Why Rust
In 2006, Graydon Hoare, a programmer at Mozilla, returned home to find his apartment elevator out of service due to software crash, which wasn't the first time. For decades developers had frustrations with the limitations and the fragility of the languages powering such systems. Popular choices like C and C++ were fast and powerful, but they made it dangerously easy to introduce memory bugs, and errors that could crash programs or open the door to security vulnerabilities. Microsoft had estimated that nearly 70% of its software flaws stem from these memory-related issues.
Hoare designed a language that could deliver both performance and safety: one that eliminated memory errors, supported concurrency, and still ran close to the metal. He called it Rust, after a resilient fungus known for its ability to thrive under harsh conditions. Rust was designed to combine speed, efficiency, and reliability, setting the stage for a new approach to programming.
How Rust went from a side project to the world’s most-loved programming language
Mozilla officially sponsored Rust in 2009, employing engineers to develop it. Released version 1.0 in 2015, marking its maturity. And now it has been widely adopted by major companies:
- Discord rewrote systems in Rust from Go for 10x speed improvements.
- Dropbox rebuilt its storage, sync engine and backend in Rust to handle billions of files.
- Rust is now a permanent language for linux kernel development
Rust has been voted the "most loved programming language" in the Stack Overflow Developer survey since 2016. Rust is now used in industries from aerospace to automotive.
Key Features That Distinguish Rust
-
Memory safety without garbage collection; memory is managed through ownership, borrowing, and lifetimes.
- Ownership: Each piece of data has a single owner, ensuring clear resource management.
- Borrowing: References allow safe data sharing without transferring ownership.
- Lifetimes: Guarantee that references remain valid, preventing dangling pointers
- These features along with strong statically type systme prevents and entire class of memory bugs at compile time.
- No double-frees, No use-after-free.
- No uninitialized variables.
-
Fearless Concurrency with safe concurrency primitives (threads, channels, async/await) to make parallel programming more reliable
- No forgotten locked mutexes. Mutexes implement the RAII (Resource Acquisition Is Initialization) pattern. When a lock guard goes out of scope, it automatically unlocks, so you can’t forget.
- No data races between threads. Rust’s type system enforces the Send and Sync traits. Shared mutable state requires synchronization, and the compiler prevents unsafe concurrent access.
-
Expressive Type System: Strong static typing with pattern matching, traits, and generics. Encourages writing concise yet powerful code that is easy to maintain
- Rust is multi-paradigm systems programming. Imperative Programming, Object Orientation through struct, enum, traits, methods and functional programming concepts like immutability, higher-order functions, pattern matching
- No NULL pointers: no issue of dereferencing null pointers, dangling pointers. Rust uses
Option<T>type to represent “maybe a value.” This forces developers to handle absence explicitly. - Good compiler error messages allows writing and debugging rust code easy and more productive.
-
Safety to prevent undefined runtime behaviors
- No iterator invalidation: Iterators borrow collections immutably or mutably in a way that prevents unsafe modifications during iteration.
- Vector and Array access is bounds checked. If out of range, Rust panics instead of silently corrupting memory.
- Integer overflow is defined, overflow causes a panic.
-
Performance & Abstractions
- Rust is statically compiled with
rustcwhich uses LLVM as its backend. Performance (runtime and memory) is comparable to C/C++. - Zero-cost abstractions (e.g., iterators): Rust’s abstractions compile down to efficient machine code. Iterators, closures, and traits are optimized away by the compiler, so they don’t add runtime overhead.
- Since Memory is managed through ownership and lifetimes. This means you allocate exactly what you need, and memory is freed deterministically when values go out of scope.
- No overhead foreign function interface (FFI). Function call be rust and C have identical performance to C function calls
- Rust is statically compiled with
-
Modern Tooling (Cargo & Crates.io)
- Cargo: Rust’s package manager and build tool, simplifying dependency management and project setup.
- Crates.io: Central repository for Rust libraries, fostering a strong open-source ecosystem
-
Cross-Platform & Embedded Friendly
- Works well for system-level programming, web assembly, and embedded devices.
- supports many platforms and architectures: x86, ARM, WASM, Linux, Mac, Windows, ...
- Supports no_std environments for microcontrollers
However, the introduction of these safety concepts also makes the learning curve steeper, but rewarding.
How to use this Guide
There are several useful keyboard shortcuts in mdBook:
Arrow-Left: Navigate to the previous page.Arrow-Right: Navigate to the next page.Ctrl + Enter: Execute the code sample that has focus.s: Activate the search bar.
There are code blocks like the following in this book which allows you to edit and run the code as you wish.
fn main() { println!("Hello 🌍!"); }
And yes it looks very like C/C++, main function define with fn keytword is the entry point of the program. Blocks are delimited by curly braces and needs a semi-colon to end a statement.