Web Analytics

Hello, World! in Rust

Beginner ~15 min read

Every programming journey begins with "Hello, World!" In this lesson, you'll write your first Rust program, understand its structure, learn about the main function and println! macro, and discover how to compile and run Rust code.

Your First Rust Program

Let's start with the simplest possible Rust program:

Output
Click Run to execute your code

This program does one thing: it prints "Hello, World!" to the console. Let's break down each part.

Anatomy of a Rust Program

The main Function

Every Rust program must have a main functionβ€”it's the entry point where execution begins:

fn main() {
    // Code goes here
}
Function Syntax:
  • fn - keyword to declare a function
  • main - the function name (special: program entry point)
  • () - parentheses for parameters (empty here)
  • {} - curly braces contain the function body

The println! Macro

println! is a macro (note the !) that prints text to the console with a newline:

println!("Hello, World!");
Macro vs Function: The ! indicates a macro, not a regular function. Macros are expanded at compile time and can do things functions can't, like take a variable number of arguments.

Statements and Semicolons

In Rust, most statements end with a semicolon ;. This tells the compiler where one statement ends and another begins.

Compiling and Running

Step 1: Create the File

Create a file named hello.rs (the .rs extension is for Rust source files):

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

Step 2: Compile with rustc

Use the Rust compiler rustc to compile your program:

rustc hello.rs

This creates an executable file:

  • Windows: hello.exe
  • macOS/Linux: hello

Step 3: Run the Program

# Windows
.\hello.exe

# macOS/Linux
./hello

You should see:

Hello, World!
Best Practice: While rustc works for simple programs, you'll typically use Cargo (Rust's build tool) for real projects. We'll cover Cargo in the next lesson!

Comments and Formatting

Let's enhance our program with comments and string formatting:

Output
Click Run to execute your code

Comments

Rust supports two types of comments:

  • Line comments: // This is a comment
  • Block comments: /* This spans multiple lines */

String Formatting

Use {} as placeholders in format strings:

let name = "Alice";
println!("Hello, {}!", name);  // Prints: Hello, Alice!

You can use multiple placeholders:

let name = "Bob";
let age = 30;
println!("{} is {} years old", name, age);

Common Mistakes

1. Forgetting the exclamation mark

Wrong:

println("Hello");  // Error: println is not a function!

Correct:

println!("Hello");  // Correct: println! is a macro

2. Missing semicolon

Wrong:

fn main() {
    println!("Hello")  // Error: missing semicolon
}

Correct:

fn main() {
    println!("Hello");  // Correct
}

3. Wrong file extension

Problem: Saving file as hello.txt or hello.rs.txt

Solution: Always use .rs extension for Rust source files

Exercise: Personalize Your Program

Task: Modify the Hello World program to greet yourself and share something about you.

Requirements:

  • Print a greeting with your name
  • Add a second line stating your favorite programming language
  • Use variables and string formatting
  • Add comments explaining your code
Output
Click Run to execute your code
Show Solution
// My personalized Rust program
fn main() {
    let name = "Alice";
    let favorite_language = "Rust";
    
    println!("Hello! My name is {}.", name);
    println!("My favorite programming language is {}!", favorite_language);
    println!("I'm excited to learn more about systems programming!");
}

Summary

  • Every Rust program starts with a main function
  • fn keyword declares functions
  • println! is a macro (note the !) for printing to console
  • Statements end with semicolons ;
  • Compile with rustc filename.rs
  • Run the resulting executable
  • Use {} for string formatting
  • Comments start with // for single lines or /* */ for blocks

What's Next?

While rustc works for simple programs, real-world Rust development uses Cargoβ€”Rust's build system and package manager. In the next lesson, you'll learn how to create projects with Cargo, manage dependencies, and use Rust's powerful tooling ecosystem.