No history yet

Introduction to Rust

Why Rust?

Rust is a modern programming language focused on three things: performance, safety, and concurrency. Think of it like building a race car. You want it to be incredibly fast, but you also want it to have excellent brakes and a strong roll cage. Many languages make you choose between speed and safety. Rust is designed to give you both.

Rust lets you write code that is both fast and correct, preventing common bugs at compile time, before your program even runs.

Its main selling point is memory safety without a garbage collector. In languages like C++, you manually manage memory, which is fast but can lead to bugs like dangling pointers or memory leaks. In languages like Python or Java, a garbage collector handles memory for you, which is safer but can introduce performance overhead. Rust uses a unique system called ownership and borrowing to manage memory at compile time, giving you the performance of manual management with strong safety guarantees.

Lesson image

Setting Up Your Environment

Getting started with Rust is straightforward. The primary tool you'll use is rustup, which is the official Rust installer and version manager. It not only installs the Rust compiler (rustc) but also comes with Cargo, Rust's build tool and package manager. Cargo handles compiling your code, managing dependencies, and running your projects.

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

After the installation is complete, you'll need to restart your terminal or run the command it provides to configure your path. You can verify that everything is working by checking the versions of the Rust compiler and Cargo.

# Check the compiler version
rustc --version

# Check the Cargo version
cargo --version

If you see version numbers for both, you're all set up and ready to write some code.

Your First Rust Program

The traditional first program for any language is "Hello, World!". Let's create one using Cargo. Cargo makes it simple to start a new project with all the necessary files and structure.

In your terminal, navigate to the directory where you want to keep your projects and run:

cargo new hello_world

This command creates a new directory called hello_world with two files inside: Cargo.toml and src/main.rs. The Cargo.toml file is the configuration file for your project, containing metadata and dependencies. Your actual code lives in the src (source) directory.

Let's look at the contents of src/main.rs:

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

This is a simple program, but let's break it down.

fn main() { ... } declares a function named main. The main function is special; it's always the first code that runs in every executable Rust program. The curly braces {} mark the body of the function.

println!("Hello, world!"); is the line that does the work. println! is a Rust macro. The exclamation mark ! indicates that you're calling a macro instead of a normal function. It prints the text inside the parentheses to the console, followed by a new line. The line ends with a semicolon ;, which indicates that this expression is over.

To run this program, navigate into your new project directory and use Cargo.

# Change into the new directory
cd hello_world

# Compile and run the program
cargo run

You should see Hello, world! printed to your terminal. The cargo run command first compiles your code into an executable and then runs it. You've just written and executed your first Rust program!

Now that you have the basics down, let's test your knowledge.

Quiz Questions 1/6

What are the three core goals that the Rust programming language is designed to prioritize?

Quiz Questions 2/6

Which command is used to create a new, structured Rust project directory?

You've successfully set up your environment and run your first piece of Rust code. You're now ready to explore the core features of the language.