Installation & Setup
Getting started with Rust is straightforward thanks to rustup
, the official Rust toolchain installer and version manager. This guide covers installation across all major platforms and setting up a productive development environment.
Installing Rust with rustup
rustup
is the recommended way to install Rust. It manages Rust versions and associated tools, making it easy to keep your toolchain up to date and switch between different versions.
Windows Installation
Option 1: Using the Windows Installer (Recommended)
- Visit rustup.rs
- Download and run
rustup-init.exe
- Follow the on-screen instructions
- Restart your terminal or command prompt
Option 2: Using the Command Line Open PowerShell and run:
Invoke-WebRequest -Uri "https://win.rustup.rs/" -OutFile "rustup-init.exe"
.\rustup-init.exe
macOS Installation
Open Terminal and run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
After installation, restart your terminal or run:
source ~/.cargo/env
Linux Installation
Open a terminal and run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Restart your terminal or source the environment:
source ~/.cargo/env
Verifying Installation
Check that Rust is properly installed:
# Check Rust compiler version
rustc --version
# Check Cargo (package manager) version
cargo --version
# Check rustup version
rustup --version
You should see output similar to:
rustc 1.75.0 (82e1608df 2023-12-21)
cargo 1.75.0 (1d8b05cdd 2023-11-20)
rustup 1.26.0 (5af9b9484 2023-04-05)
Understanding the Rust Toolchain
When you install Rust with rustup, you get several tools:
rustc: The Rust compiler
rustc hello.rs # Compile a single file
cargo: Package manager and build tool
cargo new my_project # Create new project
cargo build # Build project
cargo run # Build and run
cargo test # Run tests
rustup: Toolchain manager
rustup update # Update Rust
rustup show # Show installed toolchains
rustup default stable # Set default toolchain
Setting Up Your Development Environment
Visual Studio Code (Recommended)
VS Code has excellent Rust support through extensions:
- Install Visual Studio Code
- Install the
rust-analyzer
extension - Optionally install
CodeLLDB
for debugging
Essential VS Code Extensions:
- rust-analyzer: Language server with IntelliSense, syntax highlighting, and more
- CodeLLDB: Native debugger for Rust
- Better TOML: Better syntax highlighting for Cargo.toml files
- Error Lens: Inline error highlighting
Other IDEs and Editors
IntelliJ IDEA / CLion
- Install the official Rust plugin
- Provides excellent debugging and refactoring tools
Vim/Neovim
" Add to your .vimrc
Plug 'rust-lang/rust.vim'
Plug 'neoclide/coc.nvim' " For LSP support
Emacs
;; Add to your init.el
(use-package rust-mode)
(use-package lsp-mode)
Creating Your First Project
Use Cargo to create a new Rust project:
cargo new hello_rust
cd hello_rust
This creates a project structure:
hello_rust/
├── Cargo.toml # Project metadata and dependencies
└── src/
└── main.rs # Main source file
Cargo.toml (Project configuration):
[package]
name = "hello_rust"
version = "0.1.0"
edition = "2021"
[dependencies]
src/main.rs (Main source file):
fn main() {
println!("Hello, world!");
}
Run your project:
cargo run
Managing Rust Versions
rustup makes it easy to manage multiple Rust versions:
Install Different Toolchains
# Install stable (default)
rustup install stable
# Install beta
rustup install beta
# Install nightly
rustup install nightly
# Install specific version
rustup install 1.70.0
Switch Between Toolchains
# Set global default
rustup default stable
# Use specific toolchain for current directory
rustup override set nightly
# Use specific toolchain for one command
cargo +nightly build
Update Toolchains
# Update all installed toolchains
rustup update
# Update specific toolchain
rustup update stable
Installing Additional Components
Rust provides additional components for specific use cases:
Rust Components
# Install rustfmt (code formatter)
rustup component add rustfmt
# Install clippy (linter)
rustup component add clippy
# Install rust-src (source code for standard library)
rustup component add rust-src
# Install rls (older language server, use rust-analyzer instead)
rustup component add rls
Target Platforms
# Add WebAssembly target
rustup target add wasm32-unknown-unknown
# Add Windows target (from Linux/macOS)
rustup target add x86_64-pc-windows-gnu
# Add ARM target
rustup target add aarch64-unknown-linux-gnu
# List available targets
rustup target list
Essential Tools
Formatting and Linting
# Format code
cargo fmt
# Check for common mistakes
cargo clippy
# Check without building
cargo check
Documentation
# Build and open project documentation
cargo doc --open
# View standard library documentation
rustup doc
Testing
# Run tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run specific test
cargo test test_name
Configuration
Cargo Configuration
Create .cargo/config.toml
in your project or home directory:
[build]
# Use all available CPU cores
jobs = 0
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
[registries.crates-io]
protocol = "sparse"
Git Integration
Add to .gitignore
:
/target/
Cargo.lock # Include this for applications, exclude for libraries
Troubleshooting Common Issues
Permission Errors (Unix)
If you get permission errors, don't use sudo
. Instead:
# Fix PATH in your shell profile
echo 'source ~/.cargo/env' >> ~/.bashrc
source ~/.bashrc
Network Issues
If you're behind a firewall or proxy:
# Set proxy for rustup
export https_proxy=http://proxy.example.com:8080
export http_proxy=http://proxy.example.com:8080
# Or configure in ~/.cargo/config.toml
[http]
proxy = "proxy.example.com:8080"
Windows Linker Issues
Install Microsoft C++ Build Tools or Visual Studio with C++ development tools.
Alternatively, use the GNU toolchain:
rustup toolchain install stable-x86_64-pc-windows-gnu
rustup default stable-x86_64-pc-windows-gnu
Performance Optimization
Faster Compilation
Add to Cargo.toml
:
[profile.dev]
opt-level = 1 # Some optimizations
debug = 1 # Line tables only
[profile.dev.package."*"]
opt-level = 3 # Optimize dependencies
Alternative Linkers
Linux/macOS with LLD:
# Install lld
# Ubuntu: sudo apt install lld
# macOS: brew install llvm
# Add to .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
Windows with LLD:
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "link-arg=/DEBUG:NONE"]
Useful Cargo Commands
# Create library project
cargo new --lib my_library
# Add dependency
cargo add serde
# Remove dependency
cargo remove serde
# Update dependencies
cargo update
# Build release version
cargo build --release
# Install binary crate
cargo install ripgrep
# Show dependency tree
cargo tree
# Check for security vulnerabilities
cargo audit
# Generate Cargo.lock
cargo generate-lockfile
Next Steps
Once you have Rust installed and your environment set up:
- Work through the official Rust Book
- Try Rust by Example
- Complete Rustlings exercises
- Explore the standard library documentation
- Join the Rust community on Discord, Reddit, or the official forum
Your Rust development environment is now ready! The combination of rustup, Cargo, and a good IDE with rust-analyzer provides everything you need to write, build, test, and debug Rust applications efficiently.