🧰 Rust — Learn From Zero
Playground: Rust Playground — click, type, run.
What is Rust?
Rust is a memory-safe, systems language without a garbage collector. It prevents bugs at compile time and is blazingly fast.
- Systems & OS development
- WebAssembly
- Performance-critical services
- Embedded
Run Online — No Install
Use the playground links above.
🟢 Beginner — Core Basics
Your First Program
rust
fn main() {
println!("Hello, world!");
}Programs start in fn main().
Variables & Types
rust
let age: u32 = 36;
let height = 1.78;
let name = "Ada";
let is_student = true;Variables immutable by default; use mut.
If / Else
rust
let age = 18;
if age >= 18 {
println!("Vote");
} else {
println!("Too young");
}If is an expression (returns a value).
Loops
rust
for i in 0..5 {
println!("{i}");
}
let mut n = 3;
while n > 0 { n -= 1; }Ranges with 0..5.
Functions
rust
fn add(a: i32, b: i32) -> i32 {
a + b
}Return type after ->; last expr returned.
Vectors & HashMaps
rust
let mut v = vec![1, 2, 3];
v.push(4);
use std::collections::HashMap;Ownership applies; use mut to change.
🟡 Intermediate — Building Real Programs
Ownership
rust
let s = String::from("hi");
let t = s; // s moved
// s no longer usableEach value has one owner.
Borrowing
rust
fn len(s: &String) -> usize { s.len() }
let s = String::from("hi");
let l = len(&s);Borrow with &, no move.
Structs
rust
struct Person { name: String, age: u32 }
impl Person {
fn greet(&self) {}
}Implement methods with impl.
Traits
rust
trait Animal { fn speak(&self); }
struct Dog;
impl Animal for Dog { fn speak(&self) {} }Traits are interfaces.
Enums & Match
rust
enum Color { Red, Green }
let c = Color::Red;
match c { Color::Red => println!("red"), _ => {} }Exhaustive match.
🔴 Advanced — Pro Concepts
Result & Option
rust
let x: Result = Ok(5);
let y = x.unwrap_or(0); No exceptions; use Result/Option.
Lifetimes
rust
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } }Track references' validity.
Closures
rust
let add = |a: i32, b: i32| a + b;
println!("{}", add(2, 3));Capturing anonymous functions.
Async
rust
async fn fetch() -> String {
"data".to_string()
}Async via tokio/async-std.
What's Next?
- Build a tiny project: Calculator, To-Do App, Weather Widget
- Read the official docs and a good book
- Solve Exercism exercises
- Join a community & share your code
Practice daily for 20 minutes. Consistency > intensity.