🦀 Getting Started with Rust
Your Adventure Begins Here!
Imagine you’re about to build the coolest LEGO spaceship ever. But before you can snap those bricks together, you need a few things: the LEGO pieces, a table to work on, and some rules about how to build.
Rust is just like that! It’s a super-safe building language for computers. Today, we’ll get everything set up and learn the basic building rules.
🛠️ Installing Rust
What is This?
Installing Rust is like getting your LEGO toolbox. Without it, you can’t build anything!
How to Do It
Open your computer’s terminal (the black box where you type commands) and type:
curl --proto '=https' \
--tlsv1.2 \
-sSf https://sh.rustup.rs \
| sh
After it finishes, restart your terminal and check if it worked:
rustc --version
You should see something like: rustc 1.75.0
🎉 Congratulations! Your toolbox is ready!
📦 Cargo: Your Magic Helper
What is Cargo?
Think of Cargo as your robot assistant. It does all the boring stuff for you:
- 📁 Creates new projects (sets up your LEGO table)
- 📥 Downloads extra pieces you need (from the internet)
- 🔨 Builds your project (snaps everything together)
- 🚀 Runs your code (makes your spaceship fly!)
Why is Cargo Amazing?
Without Cargo, you’d have to do everything by hand. With Cargo, one command does it all!
cargo --version
This shows your Cargo version. It comes free with Rust!
🆕 Creating a New Project
Let’s Build Something!
Creating a project is like saying: “I want to build a new spaceship. Give me a fresh table with all the starter pieces.”
cargo new my_first_app
cd my_first_app
What just happened?
Cargo created a folder called my_first_app with:
my_first_app/
├── Cargo.toml ← Recipe book
└── src/
└── main.rs ← Your code goes here!
- Cargo.toml: Like a recipe. Lists what your project needs.
- src/main.rs: Your main building area. This is where you write code!
🏃 Building and Running Programs
Make It Go!
You wrote some code. Now let’s make it work!
Option 1: Build then Run (Two Steps)
cargo build
./target/debug/my_first_app
Option 2: Build AND Run (One Step) ⭐
cargo run
The second way is easier! Cargo builds your code AND runs it immediately.
What Does the Starter Code Say?
Open src/main.rs and you’ll see:
fn main() {
println!("Hello, world!");
}
Run it with cargo run and see:
Hello, world!
🎊 You just ran your first Rust program!
📝 Variables and Binding
What is a Variable?
A variable is like a labeled box. You put something inside and give it a name so you can find it later.
fn main() {
let age = 10;
println!("I am {} years old", age);
}
Here, age is the box name, and 10 is what’s inside.
Binding = Putting Things in Boxes
When we write let age = 10, we’re binding the value 10 to the name age.
Think of it like writing a name on a storage box and putting your toy inside!
🔒 Mutability: Can I Change It?
Rust’s Safety Rule
By default, Rust boxes are locked. Once you put something in, you can’t change it!
fn main() {
let score = 100;
score = 200; // ❌ ERROR! Can't change!
}
Unlocking the Box
If you WANT to change something later, add mut (short for “mutable”):
fn main() {
let mut score = 100;
println!("Score: {}", score);
score = 200; // ✅ This works now!
println!("New score: {}", score);
}
Why does Rust do this?
It keeps you safe! If something shouldn’t change, it CAN’T change. No surprises!
🏔️ Constants: The Unchangeable Mountains
What is a Constant?
A constant is like carving something in stone. It can NEVER EVER change!
const MAX_PLAYERS: u32 = 4;
fn main() {
println!("Max players: {}", MAX_PLAYERS);
}
Constants vs Variables
| Feature | let Variable |
const Constant |
|---|---|---|
Can use mut? |
Yes | ❌ Never |
| Needs type? | Optional | ✅ Required |
| Set at compile time? | No | ✅ Yes |
| Naming style | snake_case | SCREAMING_CASE |
When to use constants?
For values that should NEVER change, like:
- Maximum score limits
- Mathematical values (PI)
- Configuration settings
👤 Shadowing: New Box, Same Name
What is Shadowing?
Shadowing is like getting a new box with the same name and putting something different inside.
fn main() {
let x = 5;
println!("x is {}", x); // 5
let x = x + 1;
println!("x is {}", x); // 6
let x = x * 2;
println!("x is {}", x); // 12
}
Each let x creates a brand new box named x. The old box is hidden (shadowed).
Shadowing vs Mutability
Shadowing (with let):
let spaces = " "; // String
let spaces = spaces.len(); // Number! Type changed!
Mutability (with mut):
let mut spaces = " ";
spaces = spaces.len(); // ❌ ERROR! Can't change type!
Magic trick: Shadowing lets you change the TYPE. Mutability doesn’t!
🎯 Quick Summary
graph TD A["🦀 Install Rust"] --> B["📦 Use Cargo"] B --> C["cargo new project"] C --> D["cargo run"] D --> E["Write Code!"] E --> F["let x = 5"] E --> G["let mut y = 10"] E --> H["const MAX: u32 = 100"] E --> I["let x = x + 1"] F --> J["Immutable by default"] G --> K["Mutable - can change"] H --> L["Constant - never changes"] I --> M["Shadowing - new box"]
🚀 You Did It!
You’ve learned:
- ✅ Install Rust - Get your toolbox
- ✅ Cargo - Your robot helper
- ✅ Create projects -
cargo new - ✅ Build & run -
cargo run - ✅ Variables - Labeled boxes
- ✅ Mutability - Lock or unlock boxes
- ✅ Constants - Carved in stone
- ✅ Shadowing - New box, same name
You’re ready to build amazing things with Rust! 🦀✨
