PHASE 1 ← Back to Course
1 / 17
🚀

Getting Started with Rust

Install Rust, write your first program, and master Cargo, the gateway to systems programming.

1

What is Rust and Why Use It?

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. Released in 2010 and continuously developed by a vibrant open-source community, Rust combines the performance of languages like C and C++ with memory safety guarantees that prevent entire classes of bugs at compile-time.

The Three Pillars of Rust

These properties make Rust ideal for:

2

Installing Rust

The easiest way to install Rust is using rustup, which manages Rust versions and associated tools. Here's how to get started:

Installation Steps

Shell
# On macOS, Linux, or Windows (with WSL/Git Bash):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# This installs:
# - rustc: The Rust compiler
# - cargo: The package manager and build system
# - rustup: The installer and version manager

After installation, verify everything works correctly:

Shell
rustc --version
cargo --version

You should see version information for both tools. If you're on Windows, you can also download and run the installer from https://rustup.rs/.

💡

Keep Rust Up to Date

Rust updates frequently. To update your Rust installation, simply run rustup update from your terminal.

3

Your First Rust Program: Hello World

Let's write your first Rust program. Create a new directory and a file called main.rs:

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

Now compile and run it:

Shell
rustc main.rs
./main

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

First Program Complete!

You've compiled and executed your first Rust program. The fn keyword declares a function, and println! is a macro that prints text to the console.

4

Cargo: Rust's Package Manager and Build System

For anything beyond trivial programs, you'll want to use Cargo. Cargo is Rust's official package manager and build system that handles dependencies, compilation, testing, and documentation.

Creating a New Project

Create a new Cargo project with a single command:

Shell
cargo new hello_world
cd hello_world

This creates a directory structure:

Text
hello_world/
├── Cargo.toml          # Project manifest with metadata and dependencies
└── src/
    └── main.rs         # Your source code

The Cargo.toml File

This file contains your project metadata and dependencies. Here's a typical example:

TOML
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"
[dependencies]
# Add dependencies here
serde = "1.0"
tokio = { version = "1.0", features = ["full"] }

Cargo Commands

The main Cargo commands you'll use daily:

5

Building and Running a Cargo Project

Shell
# Create the project
cargo new my_app
# Navigate to the project
cd my_app
# Edit src/main.rs with your code
# Check if it compiles (fast, no binary)
cargo check
# Run during development (slower compile, good for debugging)
cargo run
# Build for production (much faster execution)
cargo build --release
# Run the release binary directly
./target/release/my_app
💡

Pro Tip

Use cargo check while developing to get fast feedback. Only use cargo build --release when you're ready to deploy or performance-test your application.

6

Your First Real Cargo Project

Let's create a slightly more interesting Cargo project that demonstrates key concepts:

Rust
// File: src/main.rs
use std::io;
fn main() {
    let mut input = String::new();
    println!("Welcome to the Rust Guessing Game!");
    println!("Enter a number between 1 and 100:");
    io::stdin()
        .read_line(&mut input)
        .expect("Failed to read line");
    let number: u32 = input
        .trim()
        .parse()
        .expect("Please enter a valid number");
    println!("You entered: {}", number);
    if number > 50 {
        println!("That's a high number!");
    } else {
        println!("That's a low number!");
    }
}

This program demonstrates several key Rust features:

Build and run it:

Shell
cargo run
← Course Home Chapter 1 of 17 Next →