No history yet

Introduction to Rust

Why Rust?

Rust is a programming language focused on three things: performance, memory safety, and concurrency. Think of it like building with high-tech Lego bricks. You can build things that are incredibly fast and complex, but the bricks are designed in a way that prevents you from making common structural mistakes. You get the power of low-level control, like in C++, without the headaches of memory bugs that can lead to crashes or security vulnerabilities.

Lesson image

It achieves this through a unique ownership system, which the compiler checks at compile time. This means many potential errors are caught before your program even runs. This focus on safety doesn't slow your code down. Rust compiles to native machine code, making it a great choice for performance-critical services, embedded devices, and command-line tools.

Installation and Setup

Getting Rust set up is straightforward. The official tool for installing and managing Rust versions is called rustup. It handles everything for you.

For Windows users, rustup provides a simple graphical installer. For macOS and Linux, you'll use a single command in your terminal.

Open your terminal and run the following command. It will download a script and start the installation process. Just follow the on-screen prompts, and choosing the default option is fine.

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

Once it's done, you may need to restart your terminal or run a provided command to update your shell's PATH. To verify that Rust is installed, you can check the version of the Rust compiler, rustc.

rustc --version

You should see output like rustc 1.78.0 (...). The version numbers might be different, but if you see a version, you're good to go.

The installation also includes Cargo, Rust's build tool and package manager. It's a powerful tool that helps you compile your code, download libraries (called 'crates'), and more. You can check its version too.

cargo --version

Your First Program

Let's write a classic "Hello, World!" program. It's a simple tradition that confirms your environment is working correctly.

First, create a new file named main.rs. All Rust files use the .rs extension.

// This is the main function, where the program starts.
fn main() {
    // println! is a macro that prints text to the console.
    println!("Hello, world!");
}

Let's 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.
  • println!("Hello, world!"); prints the string "Hello, world!" to the screen, followed by a new line. You'll notice the exclamation mark (!). This means you're calling a Rust macro, not a normal function. We'll cover macros later, but they're a common feature in Rust. Finally, the line ends with a semicolon (;), which marks the end of the expression.

Now, let's compile and run the file. From your terminal, in the same directory as your main.rs file, run the following command:

rustc main.rs

This command invokes the Rust compiler, rustc, on your source file. If everything is correct, it will produce a binary executable file. On macOS or Linux, this file will be called main. On Windows, it will be main.exe.

To run it, execute that new file:

./main

You should see Hello, world! printed to your terminal. Congratulations, you've written your first Rust program!

While using rustc directly is great for simple programs, for anything more complex, you'll almost always use Cargo to handle building and running your code. We'll explore that soon.

Quiz Questions 1/6

What are the three primary goals that Rust focuses on?

Quiz Questions 2/6

What is the name of the official tool used to install and manage Rust toolchains?

You've successfully set up your Rust environment and run your first program. You're now ready to dive into the core features of the language.