Getting Started with Rust: Hello World & Cargo

Welcome to the first post of my Rust journey!
This article walks you through setting up Rust, writing your first program, and understanding Cargo โ Rustโs package manager and build tool.
๐ฆ What You'll Learn
Installing Rust on Linux, macOS, and Windows
Writing your first Rust program:
Hello, world!
Introduction to Cargo: Rustโs package manager
๐ง Installing Rust
Rust is installed using rustup
, a command-line tool for managing Rust versions and associated tools.
โถ๏ธ Linux & macOS:
curl https://sh.rustup.rs -sSf | sh
To use Rust immediately without restarting your terminal:
source $HOME/.cargo/env
If needed, add Rust to your path manually:
export PATH="$HOME/.cargo/bin:$PATH"
Make sure a C linker is installed โ often comes with a C compiler like gcc
.
๐ช Windows:
Go to rust-lang.org/install
Install Rust and the C++ build tools for Visual Studio
(recommended: Build Tools for VS 2017)
โ Verify Installation
rustc --version
You should see something like:
rustc 1.XX.X (commit_hash YYYY-MM-DD)
๐ Writing Your First Rust Program
1. Create a Project Directory
Linux/macOS:
mkdir ~/projects && cd ~/projects
mkdir hello_world && cd hello_world
Windows PowerShell:
mkdir $env:USERPROFILE\projects\hello_world
cd $env:USERPROFILE\projects\hello_world
2. Create the Source File
Create a file named main.rs
:
fn main() {
println!("Hello, world!");
}
3. Compile and Run
Linux/macOS:
rustc main.rs
./main
Windows CMD:
rustc main.rs
.\main.exe
You should see:
Hello, world!
Congratulations, you're a Rustacean now ๐ฆ!
๐ง Understanding the Code
fn main() {
println!("Hello, world!");
}
fn main()
defines the entry point.println!
is a macro (notice the!
) that prints text to the terminal.Every line ends with
;
to mark the end of the statement.
When you build or compile your Rust project, it generates some important files. Letโs break them down:
๐ main.rs
This is your main Rust source file โ where your program starts.
It usually looks like this:
fn main() {
println!("Hello, world!");
}
๐ For projects created with Cargo, this file is found at:
src/main.rs
โ๏ธ main.exe
(on Windows)
This is the compiled executable file. It's created when you build your project using:
rustc main.rs
# OR
cargo build
On Windows, the output is:
main.exe
You can run it directly:
./main.exe
On Linux/macOS, it will simply be named main
instead of main.exe
.
๐ง main.pdb
This is a debug info file (PDB = Program Database). It's only created on Windows and contains:
Line numbers
Function names
Variable info
It helps debuggers like gdb
, lldb
, or Visual Studio Debugger map your executable back to the original Rust code.
๐ ๏ธ Itโs generated by default when you build in debug mode:
cargo build
๐ฆ Introducing Cargo
Cargo is Rustโs official tool for:
Creating new projects
Building code
Managing dependencies
Publishing packages
Cargo comes pre-installed with Rust (via rustup
).
๐ Check Cargo:
cargo --version
๐๏ธ Creating a New Project with Cargo
cargo new hello_cargo --bin
cd hello_cargo
This creates:
hello_cargo/
โโโ Cargo.toml
โโโ src/
โโโ main.rs
The Cargo.toml
file defines metadata and dependencies.src/main.rs
is where your code goes.
๐ Build & Run the Project
cargo build # Compile
cargo run # Compile & run
cargo check # Quickly check for compile errors (no binary output)
Use cargo run
when testing and cargo check
while coding frequently.
โก Build for Release
cargo build --release
This compiles with optimizations for production and outputs to target/release
.
Subscribe to my newsletter
Read articles from Aviral directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
