A step-by-step tutorial to install Rust and create, run, test, and build your first project using Cargo.
Getting started with Rust is quick if you follow a few simple steps. In this guide, you’ll install Rust, scaffold a new project, write a tiny program, run tests, and produce a release build.
I’ll keep it minimal and practical so you can get productive fast.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustc --version
cargo --version
Rust uses cargo
for building, testing, and dependency management.
cargo new hello_rust
cd hello_rust
This creates a basic structure:
hello_rust/
├─ Cargo.toml
└─ src/
└─ main.rs
Open src/main.rs
and print a greeting:
fn main() {
println!("Hello, Rust!");
}
Run it:
cargo run
If you add dependencies to Cargo.toml
, build to install them:
cargo build
You generally won’t need this for the very first “hello world,” but it’s essential once you pull in crates like HTTP clients or web frameworks.
Rust ships a formatter:
cargo fmt
Run it from the project root to format all sources.
For a beginner-friendly pattern, wrap your code in a module and test it:
// src/main.rs
mod hello_rust {
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
}
fn main() {
println!("{}", hello_rust::greet("Rust"));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_greet_default() {
assert_eq!(hello_rust::greet("Rust"), "Hello, Rust!");
}
#[test]
fn test_greet_custom() {
assert_eq!(hello_rust::greet("World"), "Hello, World!");
}
}
Run tests:
cargo test
# or verbose:
cargo test -- --nocapture
Two common ways to build:
cargo build
cargo build --release
Run your binary:
./target/debug/hello_rust # debug
./target/release/hello_rust # release
cargo tree
Clippy is Rust’s official linter.
cargo clippy
cargo clippy --fix
Here’s a simple flow of your first project:
flowchart LR A[Install Rust] --> B[cargo new hello_rust] B --> C[Edit src/main.rs] C --> D[cargo run] D --> E[cargo test] E --> F[cargo fmt] F --> G[cargo build --release] G --> H[Run ./target/release/hello_rust]
You’ve just created, tested, and built your first Rust project. Keep it small, iterate quickly, and enjoy the safety and performance of Rust.