Intermediate

Hello World in Rust

Your first Rust program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Let’s write our first Rust program.

The Code

Create a file named hello.rs:

1
2
3
fn main() {
    println!("Hello, World!");
}

Understanding the Code

  • fn main() - The entry point of every Rust executable. fn declares a function.
  • println! - A macro (note the !) that prints to standard output with a newline. Macros are distinguished from functions by the exclamation mark.
  • "Hello, World!" - A string literal. Rust strings are UTF-8 encoded.

Running with Docker

The easiest way to run this without installing Rust locally:

1
2
3
4
5
# Pull the official Rust image
docker pull rust:1.83

# Compile and run the program
docker run --rm -v $(pwd):/app -w /app rust:1.83 sh -c "rustc hello.rs && ./hello"

Running Locally

If you have Rust installed (via rustup):

1
2
3
4
5
# Compile
rustc hello.rs

# Run
./hello

Or use Cargo (Rust’s build tool) for a proper project:

1
2
3
4
5
6
# Create a new project
cargo new hello_world
cd hello_world

# Run (compiles automatically)
cargo run

Expected Output

Hello, World!

Key Concepts

  1. Rust is compiled - Source code (.rs) is compiled directly to machine code, no VM needed
  2. main() is special - Every executable must have a main function as its entry point
  3. Macros vs Functions - println! is a macro; regular functions don’t have !
  4. Semicolons matter - Statements end with semicolons; expressions don’t
  5. Cargo is preferred - While rustc works, real projects use Cargo for dependency management

Why println! Is a Macro

You might wonder why println! has that !. In Rust, macros can:

  • Accept variable numbers of arguments
  • Do compile-time string formatting
  • Generate code at compile time

println! uses these capabilities to provide type-safe formatting without runtime overhead.

A Slightly More Complex Example

1
2
3
4
fn main() {
    let name = "World";
    println!("Hello, {}!", name);
}

This demonstrates:

  • let - Variable binding (immutable by default)
  • {} - Format placeholder (like %s in C or {} in Python)

Next Steps

Continue to Variables and Data Types to learn about Rust’s ownership system and how it handles data.

Running Today

All examples can be run using Docker:

docker pull rust:1.83
Last updated: