Featured image of post Rust in a Nutshell

Rust in a Nutshell

Rust in a Nutshell

Rust is a statically typed, compiled programming language designed to be safe, concurrent, and blazingly fast (yes, they actually say “blazingly fast” in their documentation). It has a reputation for memory safety without garbage collection, making it one of the most powerful and beloved modern programming languages.

History & Motivation of Rust

Rust was started as a personal project by Graydon Hoare in 2006 and later sponsored by Mozilla. It officially hit 1.0 in 2015 and has been steadily growing in popularity ever since.

The goal? To create a systems programming language without the footguns of C and C++.

  • C++ is fast but dangerous – forget to free memory? Boom. Use-after-free? Double boom. Rust fixes that.
  • Java has a garbage collector – Rust says, “Nah, we got this.”
  • Go has concurrency – Rust says, “Hold my beer and check out async/await.”

Rust’s core mission was to make it easy to write safe, efficient code without sacrificing control. And, oh boy, did they deliver.

More Rust History (Wikipedia)

Key Design Goals

  • Memory Safety Without Garbage Collection

    • Rust prevents segmentation faults at compile time. No null pointers, no data races, no undefined behavior.
    • This makes it much safer than C and C++ without sacrificing performance.
  • Fearless Concurrency

    • Rust’s concurrency model ensures thread safety at compile time.
    • Say goodbye to race conditions and deadlocks (or at least minimize them drastically).
  • Performance

    • Rust is as fast as C++ but with better safety guarantees.
    • If Go is “fast,” then Rust is “warp speed.”
  • Expressive and Modern Syntax

    • Rust has a rich type system, pattern matching, and powerful functional programming features.

Code Examples

Hello World in Rust

1
2
3
fn main() {
    println!("Hello, World!");
}

Variables and Constants

1
2
3
4
5
6
7
fn main() {
    let name = "Alice";
    let age = 30;
    const COUNTRY: &str = "Wonderland";
    
    println!("Name: {}, Age: {}, Country: {}", name, age, COUNTRY);
}

Ownership & Borrowing (THE CORE OF RUST)

1
2
3
4
5
6
7
8
9
fn main() {
    let s = String::from("Hello");
    takes_ownership(s);
    // println!("{}", s); // ERROR: s has been moved!
}

fn takes_ownership(some_string: String) {
    println!("{}", some_string);
} // some_string is dropped here

Structs & Traits

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

fn main() {
    let rect = Rectangle { width: 10, height: 5 };
    println!("Area: {}", rect.area());
}

Concurrency (Async/Await)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let task = tokio::spawn(async {
        sleep(Duration::from_secs(1)).await;
        println!("Hello from async task!");
    });
    
    println!("Hello from main thread!");
    task.await.unwrap();
}

Rust vs Other Languages

1. Rust vs C++

  • Memory Safety: Rust eliminates memory leaks and undefined behavior at compile time.
  • Borrow Checker: Prevents dangling references. C++ just says, “Good luck.”
  • Concurrency: Rust’s ownership model makes multi-threading easier and safer than C++.

2. Rust vs Python

  • Speed: Rust is much faster than Python (compiled vs interpreted).
  • Memory Safety: Rust ensures safety at compile time, Python relies on developers not screwing up.
  • Concurrency: Rust’s async model is more powerful than Python’s threading model.

3. Rust vs Java

  • No Garbage Collector: Rust doesn’t need one, Java does.
  • Performance: Rust outperforms Java in raw execution speed.
  • Concurrency: Rust’s async/await model is lighter and faster than Java’s threads.

4. Rust vs Go

  • Speed: Rust is faster than Go because it compiles to native machine code without a runtime.
  • Memory Management: Go has garbage collection, Rust does not (and doesn’t need it).
  • Concurrency: Go’s goroutines are easier for simple cases, but Rust gives fine-grained control over threading and async execution.

What Rust is Good For

  1. Systems Programming – Rust was literally built for this.
  2. Embedded Development – No garbage collector means Rust can run on bare metal.
  3. WebAssembly – Rust is one of the best languages for WebAssembly (Wasm).
  4. CLI Tools – Rust’s speed and memory efficiency make it perfect for fast command-line applications.
  5. Game Development – Engines like Bevy and Amethyst use Rust.
  6. Networking – Rust is used in networking libraries like Tokio for high-performance async applications.

What Rust is Not Good For

  1. Rapid Prototyping – Rust’s strict type system slows down fast experimentation (compared to Python).
  2. Garbage-Collected Workloads – If you rely on GC for your application, Rust might not be the best fit.
  3. Simple Web APIs – If you just need a quick web API, Go or Python might be easier to get up and running.

Key Ideas Table

ConceptExplanation
Memory SafetyNo null pointers, no segfaults, safe memory access
ConcurrencyThread safety at compile-time with async/await support
PerformanceRust is as fast as C++ but with better safety
Ownership ModelPrevents data races and undefined behavior
No Garbage CollectorMemory management without a runtime GC

GO LEARN RUST NOW!!! 🚀

====================

GO OUT AND LEARN THIS LANGUAGE NOW!

Code Examples

Hello World in Rust

1
2
3
fn main() {
    println!("Hello, World!");
}

Variables and Constants

1
2
3
4
5
6
7
fn main() {
    let name = "Alice";
    let age = 30;
    const COUNTRY: &str = "Wonderland";
    
    println!("Name: {}, Age: {}, Country: {}", name, age, COUNTRY);
}

Ownership & Borrowing (THE CORE OF RUST)

1
2
3
4
5
6
7
8
9
fn main() {
    let s = String::from("Hello");
    takes_ownership(s);
    // println!("{}", s); // ERROR: s has been moved!
}

fn takes_ownership(some_string: String) {
    println!("{}", some_string);
} // some_string is dropped here

Structs & Traits

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

fn main() {
    let rect = Rectangle { width: 10, height: 5 };
    println!("Area: {}", rect.area());
}

Concurrency (Async/Await)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let task = tokio::spawn(async {
        sleep(Duration::from_secs(1)).await;
        println!("Hello from async task!");
    });
    
    println!("Hello from main thread!");
    task.await.unwrap();
}