Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Your First Program (Hello World) | Rust Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 3 of 50

·rust

Your First Program (Hello World)

10 minutes

Course progress6%

Your First Program: Hello, World!

The classic "Hello, World!" program demonstrates the fundamental structure of a Rust application. It's simple yet introduces key concepts.

The Minimal Program

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

Breaking It Down

The main Function

Where every executable Rust program starts:

fn main() {
    // Program execution begins here
}
  • fn declares a function
  • main is special—automatically called when the program starts
  • Functions without explicit return types implicitly return () (unit type)

The println! Macro

println! is a macro (note the !), not a regular function. Macros are more powerful:

println!(\"Hello\");              // Simple print
println!(\"Hello, {}\", name);   // String interpolation
println!(\"x = {}, y = {}\", x, y); // Multiple arguments
println!(\"Debug: {:?}\", var);   // Debug formatting

Macros are identified by ! and expanded at compile time.

Running Your Program

cargo run

Output:

Hello, world!

Exploring println!

fn main() {
    // Basic output
    println!(\"Welcome to Rust!\");
    
    // Variables in output
    let name = \"Alice\";
    let age = 30;
    println!(\"Name: {}, Age: {}\", name, age);
    
    // Debug output
    let values = vec![1, 2, 3];
    println!(\"Values: {:?}\", values);
    
    // Empty line
    println!();
    
    // Expressions
    println!(\"2 + 2 = {}\", 2 + 2);
}

Important Details

Semicolons: The semicolon matters in Rust:

println!(\"Hello\");  // Statement—no return value
println!(\"Hello\")   // Returns unit type (rarely used)

Borrowing: println! borrows values rather than taking ownership, so you can use them afterwards.

Common Mistakes

  1. Forgetting parentheses: println!\"Hello\"; won't compile
  2. Wrong argument count: Missing arguments for placeholders
  3. Unescaped quotes: Inside strings, use \\\" for quotes

What's Next

Now you understand the program structure: a main function that executes first and println! for output. You're ready for variables, functions, and control flow.

Make it stickPractice what you've learned1 quiz · 1 practical test · Optional activities from Rust FundamentalsBrowse activities
Previous lessonSetting up Rust and CargoContinue to lesson 4Understanding Cargo ProjectsBack to Rust Fundamentals curriculum