
From Zero to Ownership: Your First Steps into Rust
Why Rust? Because memory bugs have ruined enough weekends.
If you've ever written C or C++ and stared at a segfault at 2 a.m., or if you've wrestled with a use-after-free that only manifests in production — you already know the pain. These bugs are not a sign of incompetence. They're an inevitable consequence of languages that hand you a loaded gun and wish you luck. Python and JavaScript sidestep this by managing memory for you, but that safety net comes with a runtime cost and a whole different class of performance trade-offs.
Enter Rust. It promises what once seemed impossible: the raw performance of C, the memory safety of a managed language, and zero garbage collector. The secret weapon is a compile-time concept called ownership — a set of rules the compiler enforces so you never ship a dangling pointer again. This guide will take you from a blank terminal to a working Rust program, and along the way you'll understand exactly why Rust developers wear that smug smile.

Installing Rust
The official way to install Rust is through rustup, a toolchain manager that handles versions, targets, and updates. It's the first thing every Rustacean installs.
Linux / macOS:
# Run in your terminal
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Windows:
# Download and run the installer from:
https://rustup.rs
# Then follow the on-screen prompts
After installation, open a new terminal and confirm everything is working:
rustc --version
cargo --version
rustc 1.78.0 (9b00956e5 2024-04-29)
cargo 1.78.0 (54d8815d0 2024-03-26)
🎉 rustc is the compiler; cargo is Rust's all-in-one build tool, package manager, and test runner. You'll use cargo for almost everything.
Hello, World!
Let's create our first project. Cargo scaffolds everything for us:
cargo new hello_rust
cd hello_rust
Cargo creates a src/main.rs with a starter program. Open it — it already has your first Rust code:
// src/main.rs
fn main() {
println!("Hello, world!");
}
Compiling hello_rust v0.1.0
Finished dev [unoptimized + debuginfo] target(s) in 0.42s
Running `target/debug/hello_rust`
Hello, world!
A few things to notice: fn declares a function. println! ends with a bang (!) — that's how you spot a macro in Rust, which is different from a regular function call. And Cargo compiled, linked, and ran your program in one step. This is your new workflow.
Ownership in 5 Minutes
Ownership is Rust's superpower. Instead of a garbage collector or manual free(), Rust uses three rules enforced at compile time:
- Every value has exactly one owner.
- When the owner goes out of scope, the value is dropped (freed).
- Ownership can be moved or borrowed, but never aliased unsafely.
Think of ownership like a library card — only one person can hold it at a time. If you hand it to someone else (move), you no longer have it.
// src/main.rs — Ownership: Move
fn main() {
let s1 = String::from("hello");
let s2 = s1; // s1 is MOVED into s2
// println!("{}", s1); // ❌ compile error — s1 no longer valid
println!("{}", s2); // ✅ s2 owns it now
}
hello
What if you want to use a value without taking ownership? Use a reference — this is called borrowing:
// src/main.rs — Borrowing
fn print_len(s: &String) {
println!("Length: {}", s.len());
}
fn main() {
let s = String::from("Rustacean");
print_len(&s); // borrow s — s still valid after this call
println!("Still have: {}", s);
}
Length: 9
Still have: Rustacean
When you break the rules, the compiler catches it before you even run the code. Here's what a real Rust error looks like:
// This code won't compile!
fn main() {
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1); // s1 was moved!
}
--> src/main.rs:5:20
|
| let s1 = String::from("hello");
| -- move occurs because `s1` has type `String`
| let s2 = s1;
| -- value moved here
| println!("{}", s1);
| ^^ value borrowed here after move
Notice how friendly that error is. Rust doesn't just say "error" — it tells you exactly where the value moved and why you can't use it. This is a feature, not a quirk. You'll grow to love it.

Common Data Types
Rust is statically typed, but the compiler is smart enough to infer types most of the time. Let's explore the basics:
Variables & Scalars
// src/main.rs — Variables
fn main() {
let x: i32 = 42; // immutable integer
let mut y: f64 = 3.14; // mutable float
y += 1.0;
let flag: bool = true;
let letter: char = '🦀'; // yes, Rust chars are Unicode!
println!("x={} y={:.2} flag={} letter={}", x, y, flag, letter);
}
x=42 y=4.14 flag=true letter=🦀
Tuples & Arrays
// src/main.rs — Tuples and Arrays
fn main() {
// Tuple: fixed size, mixed types
let point: (i32, i32, &str) = (10, 20, "origin");
println!("Point: ({}, {}) — {}", point.0, point.1, point.2);
// Array: fixed size, same type
let temps: [f32; 4] = [22.1, 24.3, 19.8, 21.0];
println!("First temp: {}°C", temps[0]);
println!("Temps count: {}", temps.len());
}
Point: (10, 20) — origin
First temp: 22.1°C
Temps count: 4
Structs
// src/main.rs — Structs
struct User {
name: String,
age: u32,
active: bool,
}
fn main() {
let user = User {
name: String::from("Ada"),
age: 34,
active: true,
};
println!("{} (age {}) — active: {}", user.name, user.age, user.active);
}
Ada (age 34) — active: true
Control Flow
if / else and loop / while / for
// src/main.rs — Control Flow
fn main() {
// if/else — no parentheses needed!
let score = 85;
let grade = if score >= 90 { "A" } else if score >= 80 { "B" } else { "C" };
println!("Grade: {}", grade);
// for loop over a range
for i in 1..=3 {
println!(" Iteration {}", i);
}
// while loop
let mut count = 0;
while count < 3 {
count += 1;
}
println!("Counted to: {}", count);
}
Grade: B
Iteration 1
Iteration 2
Iteration 3
Counted to: 3
Rust's crown jewel for control flow is match — exhaustive pattern matching that makes switch statements look like caveman tools:
// src/main.rs — match
fn describe(n: i32) -> &'static str {
match n {
1 => "one",
2 | 3 => "two or three",
4..=9 => "four to nine",
_ => "something else", // _ is the catch-all
}
}
fn main() {
for n in [1, 3, 7, 42] {
println!("{} is {}", n, describe(n));
}
}
1 is one
3 is two or three
7 is four to nine
42 is something else
Functions and Error Handling
Rust has no exceptions. Instead, fallible functions return a Result<T, E> type — either Ok(value) or Err(error). This forces you to handle errors explicitly, which eliminates entire classes of runtime surprises.
The ? operator is syntactic sugar: it unwraps Ok values and returns early with the Err if something went wrong — making error propagation clean and readable.
// src/main.rs — Result and ? operator
use std::num::ParseIntError;
fn double_number(s: &str) -> Result<i32, ParseIntError> {
let n: i32 = s.trim().parse()?; // ? propagates error on failure
Ok(n * 2)
}
fn main() {
match double_number("21") {
Ok(n) => println!("Success: {}", n),
Err(e) => println!("Error: {}", e),
}
match double_number("abc") {
Ok(n) => println!("Success: {}", n),
Err(e) => println!("Error: {}", e),
}
}
Success: 42
Error: invalid digit found in string
There's also Option<T> for values that may or may not exist — similar to null, but you can't accidentally forget to check it. It's either Some(value) or None. Together, Result and Option make Rust codebases remarkably reliable.
Your First Mini Project: Number Guessing Game
Let's put it all together. We'll build a classic CLI guessing game — it generates a random number between 1 and 100, then gives the user clues until they guess correctly. First, add the rand crate to your dependencies:
# Cargo.toml
[dependencies]
rand = "0.8"
Now, the full game:
// src/main.rs — Number Guessing Game
use std::io::{self, Write};
use std::cmp::Ordering;
use rand::Rng;
fn main() {
let secret = rand::thread_rng().gen_range(1..=100);
let mut attempts = 0;
println!("🦀 Welcome to the Rust Guessing Game!");
println!(" Guess a number between 1 and 100.\n");
loop {
let mut input = String::new();
print!("Your guess: ");
io::stdout().flush().unwrap();
io::stdin().read_line(&mut input).unwrap();
let guess: u32 = match input.trim().parse() {
Ok(n) => n,
Err(_) => {
println!(" ⚠️ Please enter a valid number.\n");
continue;
}
};
attempts += 1;
match guess.cmp(&secret) {
Ordering::Less => println!(" 📉 Too low! Try higher.\n"),
Ordering::Greater => println!(" 📈 Too high! Try lower.\n"),
Ordering::Equal => {
println!(
" 🎉 Correct! The number was {}. You got it in {} attempt(s)!",
secret, attempts
);
break;
}
}
}
}
🦀 Welcome to the Rust Guessing Game!
Guess a number between 1 and 100.
Your guess: 50
📉 Too low! Try higher.
Your guess: 75
📈 Too high! Try lower.
Your guess: 63
🎉 Correct! The number was 63. You got it in 3 attempt(s)!
Look at what you just built: random number generation, user input, parsing with error handling, comparison with match, and a game loop — all in under 40 lines of safe, fast, dependency-managed Rust. Let that sink in. 🦀

What's Next?
You've written real Rust. You've understood ownership, borrowed values, matched patterns, handled errors, and shipped a working program. That's not nothing — most people get scared off before they reach this point. You didn't.
Here's where to go from here:
The Rust Book The Rust Book
The official, free, comprehensive guide to Rust. It's exceptionally well-written — genuinely one of the best language references in the industry.
→ doc.rust-lang.org/bookRustlings
A collection of small exercises that teach Rust through hands-on fixing of broken code. Install it with Cargo and start learning by doing:
→ github.com/rust-lang/rustlingscrates.io
The Rust package registry — over 140,000 libraries (crates) covering everything from web servers to embedded systems. Whatever you want to build, someone has likely started a crate for it.
→ crates.ioThe Rust community is famously welcoming — the Rust Users Forum and the official Discord are great places to ask questions without judgment. Rustaceans genuinely love helping beginners.
The compiler might feel strict at first. You'll argue with it. You'll Google the error messages. But every time the compiler stops you, it's preventing a real bug from reaching production. Over time, the friction becomes fluency — and you'll start writing code you trust. That's the promise of Rust. Welcome to the crab club. 🦀
More from the Blog

How to Install Fusion 360 that doesn't expire
Want to use Fusion 360 for free without the 30-day limit? Learn how to legally download and install the official, non-expiring Fusion 360 personal license.

How to Train and Test An AI model on edge Impulse
This blog includes how to train an Object Localization Model on Edge impulse. We will train this model to recognize cubes of different colors. The blog will also include some tips to getting better Test Results from your model.

3D Modelling & Printing – Step by Step
TDesign cubes that will be used to Train a basic Edge impulse AI model to classify Cubes (Further Used with Robotic Arm) - Skills: 3D modelling, 3D printing