Rust is a systems programming language focused on memory safety, performance, and concurrency. Rust achieves memory safety without a garbage collector through its ownership and borrowing system, which enforces at compile time that there are no dangling pointers, buffer overflows, or data races. This makes Rust suitable for building high-performance systems software where both safety and speed are critical: web assembly, game engines, operating system components, network services, and CLI tools. Cargo is Rust’s integrated package manager and build tool — it compiles projects, manages dependencies from crates.io (the Rust package registry), runs tests, and generates documentation. This guide covers installing Rust and Cargo on RHEL 9 using rustup, the official Rust installer, and building a sample project.
Prerequisites
- RHEL 9 with sudo/root access
- gcc (C compiler, required for linking):
dnf install -y gcc
Step 1 — Install Rust with rustup
# Install required build dependencies
dnf install -y gcc make
# Install rustup (the official Rust toolchain installer)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
# Load the Rust environment into current shell
source "$HOME/.cargo/env"
# Verify
rustc --version
cargo --version
Step 2 — Configure Rust Environment
# Add to ~/.bashrc for persistence
echo 'source "$HOME/.cargo/env"' >> ~/.bashrc
source ~/.bashrc
# Check installed toolchains
rustup toolchain list
# Update Rust to the latest stable
rustup update stable
Step 3 — Create and Build a Cargo Project
cargo new --bin myapp
cd myapp
# Project structure:
# Cargo.toml — project manifest and dependencies
# src/main.rs — application source code
cat src/main.rs
# src/main.rs
fn main() {
println!("Hello, RHEL 9!");
// Rust's ownership prevents common bugs at compile time
let mut numbers = vec![1, 2, 3, 4, 5];
numbers.push(6);
let sum: i32 = numbers.iter().sum();
println!("Sum: {}", sum);
}
# Build and run
cargo build # Debug build (fast compile, larger binary)
cargo build --release # Release build (optimised, slower compile)
cargo run # Build + run in one step
./target/release/myapp # Run the optimised binary
Step 4 — Add Dependencies (Crates)
# Cargo.toml — add dependencies
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.12", features = ["json"] }
# Download and compile dependencies
cargo build
Step 5 — Run Tests
cargo test # Run all tests
cargo test --release # Run tests with optimisations
cargo test -- --nocapture # Show println! output in tests
cargo doc --open # Generate and open documentation
Step 6 — Install a Cargo Binary Tool
# Install useful Rust CLI tools
cargo install ripgrep # Fast file search (rg)
cargo install fd-find # Fast file finder (fd)
cargo install bat # cat with syntax highlighting
cargo install cargo-edit # cargo add/remove/upgrade commands
rg --version
fd --version
Conclusion
Rust and Cargo on RHEL 9 provide a modern systems programming toolchain with compile-time memory safety guarantees. The rustup installer is the recommended installation method over distribution packages because it allows managing multiple Rust toolchain versions (stable, beta, nightly) and cross-compilation targets. Release builds (cargo build --release) apply aggressive optimisations including inlining, loop unrolling, and dead code elimination — always use release builds for production binaries.
Next steps: How to Install Go on RHEL 9, How to Build a Web API with Actix-Web in Rust on RHEL 9, and How to Install Docker on RHEL 9.