Rust Tutorial
A friendly Rust intro — ownership, structs, error handling, and a command-line tool.
Rust gives you C-like control with memory safety guaranteed at compile time — no garbage collector required.
Your first Rust program
fn main() {
let name = "world";
println!("Hello, {}!", name);
}
Compile and run: rustc main.rs && ./main.
Ownership in one minute
Rust’s borrow checker enforces that each value has exactly one owner. This prevents data races and use-after-free bugs.
fn main() {
let s = String::from("hello");
let len = calculate_length(&s); // borrow, don't move
println!("'{s}' has {len} chars");
}
fn calculate_length(s: &String) -> usize {
s.len()
}
You don’t manage memory by hand and you don’t pay GC overhead — the compiler inserts the frees for you.
Structs and methods
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() {
let r = Rectangle { width: 30, height: 50 };
println!("area = {}", r.area());
}
Error handling with Result
use std::fs;
fn read_config(path: &str) -> Result<String, std::io::Error> {
fs::read_to_string(path)
}
fn main() {
match read_config("config.toml") {
Ok(text) => println!("{}", text),
Err(e) => eprintln!("failed: {}", e),
}
}
Build a CLI with Cargo
cargo new greeting
cd greeting
cargo run
Cargo handles dependencies, builds, tests, and docs — it’s the reason Rust feels modern.