No history yet

Introduction to Rust

What is Rust?

Rust is a modern programming language focused on three things: safety, speed, and concurrency. It's a systems programming language, which means it's useful for tasks where performance is critical, like game engines, operating systems, and web browsers.

Lesson image

What makes Rust special is its ability to prevent common programming errors at compile time. Before your program even runs, Rust's compiler checks for issues that could lead to crashes or security vulnerabilities. It achieves this memory safety without a "garbage collector"—a background process that cleans up memory in other languages like Java or Python. This absence of a garbage collector gives you more predictable performance.

Think of the Rust compiler as a very helpful, but strict, assistant who reviews your code and points out potential problems before they can cause any harm.

Getting Set Up

The best way to install Rust is by using a tool called rustup. It's a command-line tool that manages your Rust versions and associated tools. To install Rust, open your terminal and run the following command. It will guide you through the installation process.

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

This command downloads and runs a script that installs the latest stable version of Rust. Along with the compiler (rustc), the installation includes Cargo, Rust’s build system and package manager. You'll use Cargo to create projects, manage dependencies, and run your code. It's an essential part of the Rust ecosystem.

Your First Program

Let's write a classic "Hello, World!" program. With Cargo, starting a new project is simple. Navigate to the directory where you want to keep your projects and run this command:

cargo new hello_world

Cargo creates a new directory called hello_world with two files inside: Cargo.toml and src/main.rs. Cargo.toml is a configuration file for your project, and src/main.rs is where you'll write your application code. Cargo has already generated a simple program for you in src/main.rs:

fn main() {
    println!("Hello, world!");
}

Let's quickly break this down.

fn main() defines a function named main. The main function is special; it's always the first code that runs in every executable Rust program.

Inside the function, println!("Hello, world!"); prints text to the screen. println! is a Rust macro, which is a way of writing code that writes other code. You can spot a macro because it ends with an exclamation mark !.

To compile and run your program, navigate into your new project directory (cd hello_world) and use another Cargo command:

cargo run

You should see the output Hello, world! printed to your terminal. The cargo run command first compiles your code into an executable and then runs it. With just a few commands, you have a working Rust program. You've installed the toolchain, created a project, and executed it.

Quiz Questions 1/5

What are the three core goals that the Rust programming language focuses on?

Quiz Questions 2/5

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