Install Rust, write your first program, and master Cargo, the gateway to systems programming.
Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. Released in 2010 and continuously developed by a vibrant open-source community, Rust combines the performance of languages like C and C++ with memory safety guarantees that prevent entire classes of bugs at compile-time.
These properties make Rust ideal for:
The easiest way to install Rust is using rustup, which manages Rust versions and associated tools. Here's how to get started:
# On macOS, Linux, or Windows (with WSL/Git Bash):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# This installs:
# - rustc: The Rust compiler
# - cargo: The package manager and build system
# - rustup: The installer and version manager
After installation, verify everything works correctly:
rustc --version
cargo --version
You should see version information for both tools. If you're on Windows, you can also download and run the installer from https://rustup.rs/.
Rust updates frequently. To update your Rust installation, simply run rustup update from your terminal.
Let's write your first Rust program. Create a new directory and a file called main.rs:
fn main() {
println!("Hello, world!");
}
Now compile and run it:
rustc main.rs
./main
You should see Hello, world! printed to your terminal. Congratulations! You've written your first Rust program.
You've compiled and executed your first Rust program. The fn keyword declares a function, and println! is a macro that prints text to the console.
For anything beyond trivial programs, you'll want to use Cargo. Cargo is Rust's official package manager and build system that handles dependencies, compilation, testing, and documentation.
Create a new Cargo project with a single command:
cargo new hello_world
cd hello_world
This creates a directory structure:
hello_world/
├── Cargo.toml # Project manifest with metadata and dependencies
└── src/
└── main.rs # Your source code
This file contains your project metadata and dependencies. Here's a typical example:
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"
[dependencies]
# Add dependencies here
serde = "1.0"
tokio = { version = "1.0", features = ["full"] }
The main Cargo commands you'll use daily:
cargo build: Compiles your project in debug mode. Creates a binary in target/debug/.cargo run: Compiles and runs your project immediately. Useful during development.cargo build --release: Compiles with optimizations enabled. Creates a binary in target/release/. Much slower to compile but significantly faster to run.cargo test: Runs all tests in your project.cargo check: Quickly checks if your code compiles without generating a binary. Great for fast feedback during development.cargo fmt: Automatically formats your code according to Rust style guidelines.cargo clippy: A linter that provides suggestions for improving your code.cargo doc --open: Generates and opens HTML documentation for your project and dependencies.# Create the project
cargo new my_app
# Navigate to the project
cd my_app
# Edit src/main.rs with your code
# Check if it compiles (fast, no binary)
cargo check
# Run during development (slower compile, good for debugging)
cargo run
# Build for production (much faster execution)
cargo build --release
# Run the release binary directly
./target/release/my_app
Use cargo check while developing to get fast feedback. Only use cargo build --release when you're ready to deploy or performance-test your application.
Let's create a slightly more interesting Cargo project that demonstrates key concepts:
// File: src/main.rs
use std::io;
fn main() {
let mut input = String::new();
println!("Welcome to the Rust Guessing Game!");
println!("Enter a number between 1 and 100:");
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
let number: u32 = input
.trim()
.parse()
.expect("Please enter a valid number");
println!("You entered: {}", number);
if number > 50 {
println!("That's a high number!");
} else {
println!("That's a low number!");
}
}
This program demonstrates several key Rust features:
std::io module from Rust's standard librarylet mutexpect()if/elseBuild and run it:
cargo run