1. Introduction to Rust
Rust is a modern systems programming language that focuses on safety, speed, and concurrency. It was created by Graydon Hoare at Mozilla Research and first released in 2010. Rust was designed to be a safe alternative to C and C++.
Rust guarantees memory safety without using a garbage collector. It achieves this through its unique ownership system with borrowing and lifetimes. This makes Rust both fast and safe, preventing common bugs like null pointer dereferences and data races.
This course will take you from absolute beginner to confident Rust developer, covering every concept with practical, runnable code examples.
What Can Rust Do?
Rust is a systems programming language that powers critical infrastructure across many domains:
- Systems Programming — Operating systems, device drivers, embedded systems
- Web Development — WebAssembly, web servers, back-end services
- Game Development — Game engines, graphics programming
- Blockchain — Smart contracts, cryptocurrency platforms
- Embedded Systems — Microcontrollers, IoT devices, robotics
Your First Rust Program
The tradition in programming is to start by printing Hello, World! to the screen. In Rust, this requires the main function and the println! macro.
fn main() { println!("Hello, World!"); }
The fn keyword defines a function. main() is the entry point of every Rust program. println! is a macro that prints to the console with a newline. Unlike C or C++, Rust uses macros for formatted printing.
Rust Syntax vs Other Languages
Rust has a syntax that is familiar to C and C++ programmers but with many improvements. It uses curly braces for blocks, semicolons for statements, and has a strong type system.
fn main() { // This is a Rust comment — the compiler ignores this line println!("Rust uses semicolons and curly braces."); println!("Every statement ends with a semicolon."); }
The // symbol marks a single-line comment. Comments are notes for humans reading the code — the compiler completely ignores them. Rust also supports multi-line comments with /* */.
Rust Versions and Editions
Rust follows a rapid release schedule with a new stable version every 6 weeks. The language also has editions (2015, 2018, 2021, 2024) that can introduce breaking changes and new idioms.
- Rust 2015 — The first stable edition
- Rust 2018 — Added async/await, modules improvements
- Rust 2021 — Made some syntax changes and improved consistency
- Rust 2024 — The latest edition with new features
You can specify the edition in your Cargo.toml file.
Compiling and Running Rust Code
Rust is a compiled language. You write source code in a .rs file, compile it with rustc, and then run the resulting executable. However, most Rust projects use Cargo, the build system and package manager.
fn main() { println!("Compile with: rustc hello.rs"); println!("Run with: ./hello"); println!("Or use: cargo run"); }
Understanding the compile-run cycle is fundamental to working with Rust. The compiler translates your human-readable code into machine code with strong safety guarantees.
Knowledge Check
Ready to test your understanding of 1. Introduction to Rust?