No history yet

Introduction to Rust

What is Rust?

Rust is a modern programming language designed for performance and safety. Think of it as having the raw speed of languages like C++, but with guard rails that prevent common and frustrating bugs. It was created by Mozilla Research with three main goals in mind: safety, speed, and concurrency.

Lesson image

Safety in Rust means preventing entire classes of bugs related to memory management, like null pointer errors or accessing memory you shouldn't. Speed means the code you write can run as fast as possible, without a garbage collector slowing things down. Concurrency means Rust is built to handle multiple tasks at once, which is crucial for modern multi-core processors. These principles make Rust a great choice for everything from web servers and command-line tools to embedded systems.

The Ownership Model

The most unique feature of Rust is its ownership model. Instead of a garbage collector that cleans up memory periodically, or forcing you to manage memory manually, Rust uses a set of rules that the compiler checks at compile time. This system guarantees memory safety without sacrificing performance.

The rules of ownership are simple:

  1. Each value in Rust has a variable that’s called its owner.
  2. There can only be one owner at a time.
  3. When the owner goes out of scope, the value is dropped.

Let's see what this looks like in practice. When you assign a value from one variable to another, Rust considers the value "moved." The original variable is no longer valid, preventing accidental use of the same data from two places, which could lead to errors.

fn main() {
    // s1 is the owner of the string "hello"
    let s1 = String::from("hello");

    // Ownership of the string is moved from s1 to s2
    let s2 = s1;

    // This next line would cause a compile error because
    // s1 is no longer a valid owner.
    // println!("s1 is {}", s1);

    // This works fine because s2 is the current owner.
    println!("s2 is {}", s2);
}

This concept of moving ownership is fundamental. It's how Rust ensures that memory is freed exactly once when the owner goes out of scope, eliminating memory leaks and other related bugs before your program even runs.

Setting Up Your Environment

Getting started with Rust is straightforward thanks to a tool called rustup. It installs the Rust compiler and another essential tool: Cargo. Cargo is Rust's build system and package manager. It handles compiling your code, downloading the libraries your project depends on (called crates), and building those libraries.

After installing Rust via rustup, you can create a new project with a simple command:

# This command creates a new directory called 'hello_world' 
# with a basic project structure and a 'Hello, world!' program.
cargo new hello_world

Inside the hello_world directory, you'll find a Cargo.toml file. This file contains all the metadata Cargo needs for your project, like its name, version, and dependencies. The source code itself is in src/main.rs. To compile and run your program, you just need one command from the project directory:

# This command compiles and runs the executable.
cargo run

Cargo streamlines the entire development process, making it easy to manage projects of any size.

Zero-Cost Abstractions

One of Rust's core philosophies is "zero-cost abstractions." This means you can write high-level, expressive code without sacrificing runtime performance. The abstractions are compiled down to machine code that is just as efficient as if you had written the low-level logic by hand.

In Rust, conveniences like iterators and closures don't add overhead. You get to write clean, readable code with the performance of low-level programming.

For example, you could iterate over a collection of numbers using a high-level iterator method, or you could write a manual while loop. In many languages, the iterator might be slightly slower due to function call overhead. In Rust, the compiler is smart enough to optimize the iterator into the same fast machine code as the manual loop. This lets you focus on writing clear logic without worrying about performance penalties for using modern language features.

Time to check your understanding of these core concepts.

Quiz Questions 1/5

What are the three main design goals of Rust?

Quiz Questions 2/5

What is the primary function of Cargo in the Rust ecosystem?

With its focus on safety, speed, and powerful tooling, Rust provides a solid foundation for building reliable and efficient software.