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.
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.
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.
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.
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.
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:
source $HOME/.cargo/env
source ~/.cargo/env
This command configures your shell to use Rust's tools, including the compiler.
Check Rust installation
To verify the installation, run:
rustc --version
You should see the Rust compiler version output, indicating a successful installation.
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.
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.
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).
What is `rustup` used for?