Getting Started with Rust: Hello World & Cargo

AviralAviral
3 min read

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:

  1. Go to rust-lang.org/install

  2. 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.

0
Subscribe to my newsletter

Read articles from Aviral directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aviral
Aviral