Introduction to Rust (v1.90.0)

Introduction to Rust (v1.90.0)

Welcome to the exciting world of Rust programming! This chapter will introduce you to Rust, explain why it’s a valuable language to learn, briefly touch upon its history, and most importantly, guide you through setting up your development environment. By the end of this chapter, you’ll have Rust and its powerful tooling ready on your machine.

What is Rust?

Rust is a modern systems programming language that focuses on three primary goals: safety, performance, and concurrency. Developed by Mozilla and now a community-driven project, Rust aims to provide the control and performance of low-level languages like C and C++, but with guarantees of memory safety and thread safety that prevent entire classes of bugs at compile time.

Imagine a language that lets you write highly efficient code for operating systems, embedded devices, and game engines, without the common pitfalls of segmentation faults, null pointer dereferences, or data races. That’s Rust.

Why Learn Rust? (Benefits, Use Cases, Industry Relevance)

Learning Rust opens doors to many exciting opportunities and offers significant advantages:

  1. Memory Safety without Garbage Collection: This is Rust’s flagship feature. Its unique “ownership system” and “borrow checker” enforce strict rules at compile time, guaranteeing memory safety without needing a garbage collector. This means no runtime performance overhead for memory management, and crucially, no more dangling pointers or buffer overflows.
  2. Blazing Performance: Rust is designed for speed. It has zero-cost abstractions, meaning that abstractions (like iterators or closures) compile down to code that is just as fast as if you had written it manually. This makes Rust ideal for performance-critical applications.
  3. Fearless Concurrency: Writing concurrent code (code that runs multiple tasks simultaneously) is notoriously difficult and error-prone. Rust’s ownership system ensures that data races, a common and dangerous bug in concurrent programming, are prevented at compile time. This allows you to write multithreaded code with confidence.
  4. Robust Ecosystem & Tooling: Rust comes with Cargo, its official package manager and build system, which simplifies dependency management, building, testing, and even publishing your code. It also boasts rustfmt for consistent code formatting and Clippy for catching common mistakes and idiomatic suggestions.
  5. Growing Community & Industry Adoption: Rust consistently ranks as one of the “most loved” languages in developer surveys. Major tech companies like Microsoft, Amazon, Google, and Meta are increasingly adopting Rust for critical components, recognizing its benefits in reliability and performance. Use cases span:
    • Operating Systems: Components in Linux and Windows.
    • Web Services: High-performance APIs and microservices (e.g., Cloudflare, Discord).
    • WebAssembly (Wasm): Compiling Rust to run performantly in web browsers.
    • Command-Line Tools: Fast and reliable CLI utilities.
    • Embedded Systems: Memory-constrained environments.
    • Game Development: Game engines and tools.
    • Blockchain: Core infrastructure for cryptocurrencies and smart contracts.

A Brief History (Concise)

Rust began as a personal project by Graydon Hoare at Mozilla Research in 2006. Mozilla officially sponsored it in 2009. The language reached its first stable release, Rust 1.0, in 2015. Since then, it has seen continuous improvement with new stable versions released every six weeks. The language has also evolved through “editions,” which are major, backward-compatible updates to the language, with the latest being the Rust 2024 Edition stabilized in Rust 1.85.0.

Currently, the stable version of Rust is 1.90.0, released on September 18, 2025. This guide will focus on teaching you Rust with the features and best practices relevant to this and the underlying 2024 Edition.

Setting Up Your Development Environment

To start coding in Rust, you’ll need the Rust toolchain. The easiest way to get it is by using rustup, a command-line tool for managing Rust versions and associated tools.

Prerequisites

Before you install Rust, ensure you have a C compiler, specifically gcc or clang, as Rust uses these to link compiled code.

  • Linux: Install build-essential:
    sudo apt update
    sudo apt install build-essential # For Debian/Ubuntu
    sudo dnf install @development-tools # For Fedora/RHEL
    
  • macOS: Install Xcode Command Line Tools:
    xcode-select --install
    
  • Windows: The recommended way is to install “Build Tools for Visual Studio 2022”. When prompted, select “Desktop development with C++” workload. Alternatively, you can install the MinGW-w64 toolchain, but Visual Studio is generally preferred for a smoother experience. rustup will guide you through this if needed.

Step-by-Step Installation with rustup

  1. Open your terminal (or PowerShell/WSL on Windows).
  2. Run the rustup installer command:
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    
    • For Windows users, if the curl command isn’t available, you might need to download and run rustup-init.exe directly from the official Rust website: https://www.rust-lang.org/learn/get-started. Choose the appropriate architecture (x64, 32-bit, or ARM64).
  3. Follow the on-screen instructions: The installer will prompt you to choose an installation option. The default 1) Proceed with installation (default) is usually the best choice for beginners.
    • rustup downloads and installs the latest stable version of Rust, Cargo (Rust’s build tool and package manager), and other common tools.
    • It will also attempt to add Cargo’s bin directory to your system’s PATH environment variable. This is crucial for easily running Rust commands from any directory.
  4. Restart your terminal (or open a new one): This ensures that the PATH changes take effect.
  5. Verify the installation: Run these commands to check your Rust and Cargo versions:
    rustc --version
    cargo --version
    
    You should see output similar to this (version numbers might be newer):
    rustc 1.90.0 (e9515949d 2025-09-18)
    cargo 1.90.0 (e9515949d 2025-09-18)
    
    If you see these, congratulations! Rust is successfully installed.

Updating Rust

Rust is actively developed, and new stable versions are released every six weeks. To keep your Rust installation up-to-date, simply run:

rustup update

Creating Your First Rust Project with Cargo

Cargo is more than just a package manager; it’s the standard way to manage Rust projects. Let’s use it to create a new “Hello, World!” project.

  1. Create a new project directory:
    cargo new hello_rust_app
    cd hello_rust_app
    
    This command creates a new directory named hello_rust_app with a basic Rust project structure:
    hello_rust_app/
    ├── Cargo.toml
    └── src/
        └── main.rs
    
    • Cargo.toml: The manifest file, where you declare project metadata and dependencies.
    • src/main.rs: This is where your application’s main code will reside.
  2. Examine the default code (src/main.rs): Open src/main.rs in your favorite text editor. You’ll find:
    fn main() {
        println!("Hello, world!");
    }
    
    This is a simple Rust program that prints “Hello, world!” to the console.
  3. Run your project:
    cargo run
    
    You should see output similar to this:
        Compiling hello_rust_app v0.1.0 (/path/to/hello_rust_app)
        Finished dev [unoptimized + debuginfo] target(s) in X.Ys
        Running `target/debug/hello_rust_app`
    Hello, world!
    
    • cargo run first compiles your code (if it hasn’t been compiled or if changes have been made) and then executes the resulting binary.
    • The compiled binary is placed in target/debug/.
    • To compile without running, use cargo build. For a release-optimized build, use cargo build --release. The release binary will be in target/release/.

You now have a fully functional Rust development environment! In the next chapter, we’ll dive into the fundamental concepts of variables, data types, and operators.

Exercise: Personal Greeting

Modify the src/main.rs file in your hello_rust_app to print a personalized greeting.

Instructions:

  1. Open src/main.rs.
  2. Change the string inside println! to include your name or a custom message.
  3. Save the file.
  4. Run the program using cargo run.

Example:

fn main() {
    println!("Hello, Rustaceans! My name is [Your Name] and I'm learning Rust!");
}

This simple exercise ensures your setup is working and gives you a tiny taste of Rust code.