iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Intro

Rust is a systems language with a compiler that prevents memory and concurrency bugs. Fast, predictable, no garbage collector.

Rust — what it is

EXAMPLE
// ===== The values =====
// - Memory safety without a GC (ownership + borrowing)
// - Fearless concurrency (Send / Sync traits enforce safety at compile time)
// - C/C++ level performance
// - Cargo: package manager + build + test + docs, batteries included

// ===== Hello, world =====
fn main() {
    println!("hello, world");
}

// Build + run:
//   cargo new app && cd app
//   cargo run

// ===== Ownership in 60 seconds =====
fn main() {
    let s = String::from("hi");   // s owns the String
    let t = s;                      // ownership moves to t
    // println!("{}", s);          // compile error: s no longer valid
    println!("{}", t);
}

// ===== Borrowing =====
fn len(s: &str) -> usize { s.len() }  // borrow as a &str

fn main() {
    let s = String::from("hi");
    let n = len(&s);                   // pass a borrow; s still valid
    println!("{} {}", s, n);
}

// ===== Result + the ? operator =====
use std::fs::read_to_string;
fn config() -> Result<String, std::io::Error> {
    Ok(read_to_string("config.toml")?)
}

// ===== Concurrency (data races caught at compile) =====
use std::thread;
fn main() {
    let xs = vec![1, 2, 3];
    let handle = thread::spawn(move || {
        for x in xs { println!("{}", x); }
    });
    handle.join().unwrap();
}

// ===== When Rust wins =====
// - Systems software (kernels, browsers, embedded)
// - Performance-critical services
// - Replacing C/C++ in security-sensitive code
// - WASM modules with strict size/perf budgets

// ===== When Rust hurts =====
// - Quick prototypes (the compiler argues with you a lot up front)
// - Heavy refactor cycles in a small team
// - Anything where Go or Python would do — speed isn't free

// ===== Patterns to internalise =====
// - Default to &T; reach for owned T when you need to keep it
// - Use Result + ? for error propagation; reach for panic! only on unrecoverable bugs
// - Small crates with clear ownership boundaries
// - cargo clippy on every CI run

// ===== Pitfalls =====
// - Fighting the borrow checker -> refactor data flow, don't reach for unsafe
// - unwrap() everywhere -> reintroduces the panics Rust helps you avoid
// - Cloning to dodge borrows -> hidden allocations
// - Async lifetimes are notoriously subtle; lean on tokio + structured patterns

Why it matters

Rust trades up-front compiler arguments for runtime safety and speed. Ownership, borrowing, Result, and traits are the four-pack to learn. Reach for it when wrong code being impossible to compile is worth the friction — systems work, performance-critical paths, or shared libraries that must not corrupt memory.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// Rust: systems language. Ownership prevents data races at compile time.
// No GC. Performance close to C/C++.
Try it Yourself »

Exercise

Print with the formatting macro.

!("Hello");

Test yourself

Q1. Rust's memory safety is enforced…
Q2. Rust's package manager is…
Q3. A Rust function returns the last expression…

Discussion

Loading…