Introduction to Rust
Learn what makes Rust unique, set up your environment, and run your first program
Learning Objectives
- Understand the philosophy of Rust
- Install Rust using rustup
- Use Cargo to create and run a project
- Write your first Hello World in Rust
What is Rust?
Rust is a modern systems programming language focused on safety, speed, and concurrency. It was started at Mozilla Research and is now maintained by the Rust Foundation.
The Three Pillars of Rust
- Safety: Rust prevents memory errors like buffer overflows and use-after-free at compile time.
- Speed: Rust is as fast as C and C++ because it compiles to machine code and has no garbage collector.
- Concurrency: Rust’s ownership rules make it impossible to have data races in multi-threaded code.
Setting Up Your Environment
1. Install Rust via rustup
The official way to install Rust is using rustup, a tool for managing Rust versions.
- Windows: Download and run rustup-init.exe.
- macOS/Linux: Run the following in your terminal:
1curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
2. Verify Installation
Open a new terminal and run:
|
|
3. Editor Setup
We highly recommend Visual Studio Code with the rust-analyzer extension. It provides excellent autocompletion and inline error checking.
Meet Cargo: Rust’s Swiss Army Knife
cargo is Rust’s build system and package manager. You’ll use it for almost everything.
| Command | Action |
|---|---|
cargo new |
Create a new project |
cargo build |
Compile the project |
cargo run |
Compile and run in one step |
cargo check |
Quickly check code for errors without building |
cargo test |
Run your tests |
Your First Project
Let’s create a project named hello_rust.
|
|
Look at the structure:
Cargo.toml: Metadata and dependencies (like a package.json or requirements.txt).src/main.rs: Your Rust code.
Open src/main.rs:
|
|
Running the code
In your terminal, run:
|
|
You should see “Hello, Rustacean!” printed to the screen.
Summary
Today you learned:
- Why Rust is unique (Safety, Speed, Concurrency)
- How to install the Rust toolchain
- How to use Cargo to manage projects
- The structure of a basic Rust program
Practice Exercise
- Change the message in
main.rsto greet yourself. - Add a second
println!statement. - Try running
cargo checkinstead ofcargo run. What’s the difference?
Next Steps
Tomorrow, we’ll dive into Variables and Mutability—and learn why everything in Rust is “locked” by default!
Quiz
Complete this quiz with a minimum score of 80% to mark Day 1 as complete.
Discussion
Have questions or want to discuss this lesson? Join the conversation below!