Rust Installation (rustup) 🎯

beginner
15 min

Rust Installation (rustup) 🎯

Welcome to Rust Installation tutorial! In this lesson, we'll guide you step-by-step through the process of setting up your development environment for Rust using rustup. By the end, you'll have a fully functional Rust environment ready to build real-world projects. 📝 Note: This tutorial is for beginners and intermediates, so we'll explain concepts from the ground up.

What is Rust?

Rust is a modern, multi-paradigm programming language designed for performance and productivity. It's known for its focus on safety, concurrency, and zero-cost abstractions. Rust provides memory safety without requiring a garbage collector, making it a great choice for system programming, web applications, and more. 💡 Pro Tip: Rust is a promising language for the future, backed by Mozilla Research.

What is rustup?

rustup is the official Rust installation tool. It simplifies the process of setting up your Rust environment, including installing Rust, managing crates (libraries), and configuring toolchains. 📝 Note: You can think of rustup as the package manager for Rust.

Prerequisites

  • A 64-bit computer (Windows, macOS, or Linux)
  • An internet connection

Installing Rust using rustup

  1. Install curl (if not already installed)

    On macOS and Linux, curl is usually pre-installed. For Windows, download the latest version from curl.se and follow the installation instructions.

  2. Download rustup

    Open a terminal or command prompt and run the following command:

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

    This command downloads and runs the Rust installation script. It may take a few minutes to complete. 📝 Note: Make sure to use exactly the provided command to avoid any issues.

  3. Initialize your shell

    After the installation is complete, you need to initialize your shell to use Rust. Depending on your shell, run one of the following commands:

    • For Bash and Zsh: source $HOME/.cargo/env
    • For Fish: source ~/.cargo/env

    This command configures your shell to use Rust's tools, including the compiler.

  4. Check Rust installation

    To verify the installation, run:

    rustc --version

    You should see the Rust compiler version output, indicating a successful installation.

  5. Updating Rust and managing toolchains

    Use the rustup update command to update Rust to the latest stable version:

    rustup update

    You can also manage multiple toolchains (stable, nightly, beta, etc.) using rustup toolchain list and rustup install. 💡 Pro Tip: Using different toolchains allows you to work with newer features before they are stable.

Code Examples

  1. Hello, World! in Rust

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

    Save this code to a file named main.rs and run it with:

    rustc main.rs && ./main

    This code defines a simple main function and prints "Hello, World!" using the println! macro.

  2. Rust types (integers, strings, and booleans)

    fn main() { let x = 5; let y = "Hello"; let z = true; println!("x: {}", x); println!("y: {}", y); println!("z: {}", z); }

    This example demonstrates how to declare variables of different types in Rust: integers (i32), strings (&str), and booleans (bool).

Quiz

Quick Quiz
Question 1 of 1

What is `rustup` used for?