Rust - Installation & Tooling
Overview
Estimated time: 30–45 minutes
Learn how to install Rust using rustup, set up your development environment, and verify everything works correctly. We'll also explore the essential tools that come with Rust.
Learning Objectives
- Install Rust using rustup on Windows, macOS, and Linux.
- Understand the role of rustc, cargo, rustfmt, and clippy.
- Set up your preferred code editor with Rust support.
- Verify your installation and run your first Rust command.
Prerequisites
- Rust - Introduction
- Basic command line familiarity
Installing Rust with rustup
What is rustup?
rustup is the official installer and version management tool for Rust. It manages:
- Rust compiler (rustc) versions
- Standard library versions
- Additional tools like cargo, rustfmt, clippy
- Cross-compilation targets
Installation on Windows
- Visit rustup.rs
- Download and run
rustup-init.exe
- Follow the installer prompts (choose default options for beginners)
- Restart your command prompt or PowerShell
Installation on macOS
Open Terminal and run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Then restart your terminal or run:
source ~/.cargo/env
Installation on Linux
Open your terminal and run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Then restart your terminal or run:
source ~/.cargo/env
Verifying Your Installation
Check Rust Version
rustc --version
Expected Output:
rustc 1.70.0 (90c541806 2023-05-31)
Check Cargo Version
cargo --version
Expected Output:
cargo 1.70.0 (ec8a8a0ca 2023-04-25)
Test with Hello World
Create a simple program to test everything works:
mkdir rust-test
cd rust-test
echo 'fn main() { println!("Hello, Rust!"); }' > main.rs
rustc main.rs
./main # On Windows: main.exe
Expected Output:
Hello, Rust!
Essential Rust Tools
rustc - The Rust Compiler
The core compiler that transforms Rust source code into executable programs.
Basic Usage
# Compile a single file
rustc main.rs
# Compile with optimization
rustc -O main.rs
# Compile for a specific target
rustc --target x86_64-pc-windows-gnu main.rs
cargo - Package Manager and Build Tool
Cargo handles project creation, dependency management, building, testing, and publishing.
Key Commands
# Create a new project
cargo new my_project
cd my_project
# Build your project
cargo build
# Build and run
cargo run
# Run tests
cargo test
# Check if code compiles (faster than build)
cargo check
# Build with optimizations
cargo build --release
rustfmt - Code Formatter
Automatically formats your Rust code according to style guidelines.
Usage
# Format a single file
rustfmt src/main.rs
# Format entire project
cargo fmt
# Check formatting without making changes
cargo fmt -- --check
Example: Before and After
Before formatting:
fn main(){let x=5;let y=10;println!("Sum: {}",x+y);}
After running rustfmt
:
fn main() {
let x = 5;
let y = 10;
println!("Sum: {}", x + y);
}
clippy - Linter and Code Analysis
Catches common mistakes and suggests improvements to your Rust code.
Installation and Usage
# Install clippy (if not already installed)
rustup component add clippy
# Run clippy on your project
cargo clippy
# Run clippy with extra strictness
cargo clippy -- -W clippy::all
Example: Clippy Suggestions
Code with issues:
fn main() {
let x = 5;
if x == 5 {
println!("x is 5");
}
let vec = vec![1, 2, 3];
for i in 0..vec.len() {
println!("{}", vec[i]);
}
}
Clippy output:
warning: for loop over a range of indices is not idiomatic
--> src/main.rs:7:5
|
7 | for i in 0..vec.len() {
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using an iterator: `for item in &vec`
Setting Up Your Development Environment
Visual Studio Code (Recommended)
- Install VS Code
- Install the "rust-analyzer" extension
- Install the "CodeLLDB" extension for debugging
Useful VS Code Settings
Add to your settings.json
:
{
"rust-analyzer.checkOnSave.command": "clippy",
"rust-analyzer.cargo.autoreload": true,
"editor.formatOnSave": true,
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
}
}
IntelliJ IDEA / CLion
- Install IntelliJ IDEA or CLion
- Install the "Rust" plugin
- Configure toolchain in Settings → Languages & Frameworks → Rust
Vim/Neovim
For Vim users, consider these plugins:
- rust.vim: Basic syntax highlighting
- coc-rust-analyzer: LSP support with CoC
- vim-lsp: Alternative LSP client
Managing Rust Versions
rustup Commands
# Show installed toolchains
rustup show
# Update to latest stable
rustup update
# Install nightly toolchain
rustup install nightly
# Set default toolchain
rustup default stable
# Use specific toolchain for a project
rustup override set nightly
# Add target for cross-compilation
rustup target add wasm32-unknown-unknown
Rust Release Channels
- Stable: Released every 6 weeks, recommended for production
- Beta: Next stable release candidate
- Nightly: Latest features, may be unstable
Creating Your First Cargo Project
Project Structure
cargo new hello_cargo
cd hello_cargo
This creates:
hello_cargo/
├── Cargo.toml
└── src/
└── main.rs
Cargo.toml Configuration
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
# Dependencies will go here
Default main.rs
fn main() {
println!("Hello, world!");
}
Building and Running
# Build the project
cargo build
# Build and run in one command
cargo run
# Build for release (optimized)
cargo build --release
Expected Output:
Compiling hello_cargo v0.1.0 (/path/to/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.50s
Running `target/debug/hello_cargo`
Hello, world!
Understanding Cargo Build Outputs
Debug vs Release Builds
- Debug:
target/debug/
- Fast compilation, includes debug info - Release:
target/release/
- Optimized for performance
Build Artifacts
target/
├── debug/
│ ├── hello_cargo # Debug executable
│ └── incremental/ # Incremental compilation cache
└── release/
└── hello_cargo # Release executable
Common Issues and Solutions
PATH Not Updated
Problem: Command not found after installation
Solution: Restart terminal or manually add to PATH:
# Add to ~/.bashrc or ~/.zshrc
export PATH="$HOME/.cargo/bin:$PATH"
Network Issues
Problem: Download fails behind corporate firewall
Solution: Configure proxy or download offline installer
Linker Errors on Windows
Problem: Missing Visual Studio Build Tools
Solution: Install Microsoft C++ Build Tools or Visual Studio
Next Steps
Now that Rust is installed and configured:
- Create a test project with
cargo new
- Experiment with
cargo run
,cargo build
, andcargo check
- Try formatting code with
cargo fmt
- Run clippy with
cargo clippy
- Explore your editor's Rust support
Common Pitfalls
- Using rustc directly: For multi-file projects, use cargo instead
- Ignoring clippy warnings: They often catch real issues or suggest better patterns
- Not using rustfmt: Consistent formatting makes code more readable
- Sticking to stable only: Sometimes nightly features are worth exploring
Checks for Understanding
- What is rustup and why is it recommended over direct installation?
- What's the difference between
cargo build
andcargo run
? - How do you format Rust code automatically?
- What tool helps catch common mistakes in Rust code?
- What are the three main Rust release channels?
Answers
- rustup manages Rust versions, toolchains, and components, making updates and multi-version support easy
cargo build
compiles the project,cargo run
compiles and executes it- Use
rustfmt
directly orcargo fmt
for the entire project - clippy - the Rust linter that suggests improvements
- Stable, Beta, and Nightly