This chapter guides you through setting up your Rust development environment and writing your first program. By the end, you'll have a fully configured workspace and understand the basics of Cargo, Rust's build system and package manager.
Rust is installed through rustup, the official Rust toolchain installer. It manages Rust versions and associated tools, making it easy to keep your installation up to date.
Open your terminal and run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
This command downloads and runs the rustup installer. Follow the on-screen prompts (usually pressing Enter for default options is fine).
The installer will add the following to your shell configuration:
source "$HOME/.cargo/env"
source ~/.cargo/env
Visit rustup.rs and download rustup-init.exe
Rust requires the Microsoft C++ Build Tools. The installer will prompt you to install them if needed.
After installation, verify everything works:
# Check Rust compiler version rustc --version # Example output: rustc 1.75.0 (82e1608df 2023-12-21) # Check Cargo version cargo --version # Example output: cargo 1.75.0 (1d8b05cdd 2023-11-20) # Check rustup version rustup --version # Example output: rustup 1.26.0 (5af9b9484 2023-04-05)
rustup installs these essential components:
rustc - The Rust compilercargo - Build system and package managerrustup - Toolchain managerrustfmt - Code formatterclippy - Linterrust-docs - Local documentationRust releases a new stable version every six weeks. Keep your installation current:
# Update Rust to the latest version rustup update # Show installed toolchains rustup show # Install a specific version rustup install 1.70.0 # Switch default toolchain rustup default stable # Install nightly (for experimental features) rustup install nightly rustup run nightly cargo build
Use stable for production code. The nightly channel has experimental features that may change. Some tools (like rocket web framework) may require nightly.
A good IDE makes Rust development much more productive. We recommend Visual Studio Code with the rust-analyzer extension.
Download from code.visualstudio.com
Search for "rust-analyzer" in the Extensions panel (Ctrl+Shift+X) and install it.
| Feature | Description |
|---|---|
| Code completion | Smart suggestions as you type |
| Go to definition | Jump to function/type definitions (F12) |
| Inline errors | See compiler errors without building |
| Inlay hints | Show types and parameter names inline |
| Rename refactoring | Safely rename across project |
| Code actions | Quick fixes and code generation |
Let's create and run your first Rust program using Cargo.
# Create a new project called hello_rust cargo new hello_rust # Navigate into the project cd hello_rust
Cargo creates this project structure:
Open Cargo.toml - this is your project's manifest:
[package] name = "hello_rust" version = "0.1.0" edition = "2021" # See more keys and their definitions at # https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] # Your external dependencies go here
[package] - Project metadata (name, version, edition)edition - Rust edition (2015, 2018, 2021) - use latest[dependencies] - External crates your project uses[dev-dependencies] - Dependencies only for tests/examples[build-dependencies] - Dependencies for build scriptsOpen src/main.rs:
fn main() {
println!("Hello, world!");
}
Let's break this down:
| Code | Explanation |
|---|---|
fn |
Keyword to define a function |
main() |
The entry point - every Rust program starts here |
{ } |
Function body enclosed in curly braces |
println! |
A macro (note the !) that prints to console |
"Hello, world!" |
A string literal |
; |
Statements end with semicolons |
# Build and run
cargo run
# Output:
Compiling hello_rust v0.1.0 (/path/to/hello_rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.42s
Running `target/debug/hello_rust`
Hello, world!
You've just written and run your first Rust program!
Cargo is your constant companion in Rust development. Here are the essential commands:
| Command | What It Does |
|---|---|
cargo new <name> |
Create a new project |
cargo build |
Compile the project |
cargo run |
Build and run |
cargo check |
Check for errors without building (faster) |
cargo test |
Run tests |
cargo fmt |
Format code |
cargo clippy |
Run linter for code improvements |
cargo doc --open |
Generate and open documentation |
cargo build --release |
Build optimized release version |
cargo add <crate> |
Add a dependency |
# Debug build (fast compilation, slow execution, with debug symbols) cargo build # Binary: target/debug/hello_rust # Release build (slow compilation, fast execution, optimized) cargo build --release # Binary: target/release/hello_rust
Use cargo run during development for fast iteration. Use cargo build --release for performance testing and deployment. The performance difference can be 10-100x!
Rust's package ecosystem lives on crates.io. Let's add a dependency:
# Add the rand crate cargo add rand
This updates your Cargo.toml:
[dependencies] rand = "0.8.5"
Now use it in your code:
use rand::Rng;
fn main() {
let secret_number = rand::thread_rng().gen_range(1..=100);
println!("Secret number: {}", secret_number);
}
| Syntax | Meaning |
|---|---|
"0.8.5" |
Compatible with 0.8.5+ but less than 0.9.0 |
"^0.8.5" |
Same as above (caret is default) |
"~0.8.5" |
Patch updates only (0.8.5 - 0.8.x) |
"=0.8.5" |
Exactly this version |
"*" |
Any version (not recommended) |
As your project grows, organize it properly:
# Create a library project cargo new my_library --lib # Create a binary project (default) cargo new my_app
main.rs, produces an executablelib.rs, produces a library for others to userustup to install and manage Rust toolchainsCargo.toml for configuration, src/ for codecargo addrustup and why is it preferable to installing Rust from package managers?cargo build and cargo check? When would you use each?Cargo.toml file. What sections does it contain?! in println! indicate?In the next chapter, you'll learn Rust's most important concept—ownership. This is what makes Rust unique and enables memory safety without garbage collection: