What is Rust?
Rust is a systems programming language that was originally developed by Mozilla Research. It's designed to be safe, fast, and concurrent, making it perfect for building reliable and efficient software. One of the most interesting things about Rust is that it doesn't use a garbage collector like many other languages. Instead, it uses an ownership system to manage memory at compile time.
Rust has been used to build some really impressive projects. Firefox uses Rust components, Dropbox built their sync engine with it, Cloudflare runs Rust on their edge network, and Discord rewrote their Go services in Rust for better performance. The language has won Stack Overflow's "most loved language" survey for several years in a row, which shows how much developers enjoy working with it.
Whether you want to build command-line tools, web servers, game engines, or even software for embedded devices, Rust is a great choice. It gives you the low-level control of languages like C and C++ but with modern tooling and guarantees that help prevent common bugs.
// Rust focuses on safety, speed, and concurrency
fn main() {
println!("Rust is designed for reliability and performance!");
}
Why Learn Rust?
Rust solves real problems that developers face in other languages:
- No null pointer exceptions: Rust uses
Option<T>instead of null, so you handle missing values explicitly at compile time. - No data races: The ownership system prevents multiple threads from accessing the same data simultaneously without proper synchronization.
- No buffer overflows: Rust checks array bounds at runtime and panics (safely) if you go out of bounds.
- Zero-cost abstractions: High-level features like iterators and closures compile down to the same code as hand-written loops.
- Excellent tooling: Cargo (Rust's package manager and build system) handles dependencies, testing, benchmarking, and documentation generation.
Installing Rust
The recommended way to install Rust is through rustup, the official toolchain manager. It handles installing the compiler, standard library, and cargo for you:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
After installation, verify everything works:
rustc --version
cargo --version
Cargo is Rust's build system and package manager. You'll use it for almost everything โ creating projects, building code, running tests, and managing dependencies. Think of it as npm for JavaScript or pip for Python, but built into the language.
Variables and Mutability
In Rust, variables are immutable by default. This is a deliberate design choice โ it encourages you to think carefully about what should change and what shouldn't:
fn main() {
let x = 5;
// x = 6; // This would cause a compile error
let mut y = 5;
y = 6; // This works because y is mutable
println!("x = {}, y = {}", x, y);
}
You can also shadow a variable by re-declaring it with let. This is different from mutation because it creates a new variable that happens to have the same name:
let space = " "; // &str
let space = space.len(); // usize โ different type!
Data Types
Rust has two main categories of data types: scalar and compound.
Scalar types represent a single value:
// Integers: i8, i16, i32, i64, i128, isize
// Unsigned: u8, u16, u32, u64, u128, usize
let age: u32 = 25;
// Floating point: f32, f64
let pi: f64 = 3.14159;
// Boolean
let is_active: bool = true;
// Character (4 bytes, supports Unicode)
let letter: char = 'A';
let emoji: char = '๐ฆ';
Compound types group multiple values:
// Tuple โ fixed size, mixed types
let person: (&str, i32) = ("Alice", 30);
let (name, age) = person; // destructuring
// Array โ fixed size, same type
let scores = [85, 90, 78, 92, 88];
let first = scores[0];
Functions and Control Flow
Functions in Rust use fn and must declare parameter types. The return type comes after an arrow, and the last expression (without a semicolon) is the return value:
fn add(a: i32, b: i32) -> i32 {
a + b // no semicolon = return value
}
fn main() {
let result = add(5, 3);
println!("5 + 3 = {}", result);
// If expressions
let number = 7;
if number % 2 == 0 {
println!("Even");
} else {
println!("Odd");
}
// Match expression (like switch on steroids)
match number {
1 => println!("One"),
2..=5 => println!("Two to five"),
6..=10 => println!("Six to ten"),
_ => println!("Something else"),
}
}
The match expression is one of Rust's most powerful features. It must cover every possible value, which means the compiler catches missing cases at compile time rather than at runtime.