Lesson 1

Hello, Rust!

Write your first Rust program and learn about fn main() and the println! macro.

Welcome to Rust

Every Rust program starts with a fn main() function -- it's the entry point, the first thing that runs. Let's write one right now.

To print text to the screen, Rust uses println!. That exclamation mark isn't a typo -- it means println! is a macro, not a regular function. For now, just think of it as Rust's way of printing. You'll learn what macros really are later.

The Basics

Here's what you need to know:

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

fn declares a function. main is its name. The curly braces {} wrap the function body. Every statement ends with a semicolon.

You can print multiple things, and you can use {} inside the string as a placeholder for values -- like a template. Hit Run to see it in action.

Try it

fn main() {
    println!("Hello, world!");
    println!("Welcome to Rust!");

    let year = 2024;
    println!("Rust has been growing fast since {}.", year);
    println!("{} + {} = {}", 2, 3, 2 + 3);
}

Challenge

Try printing your name! Use println! with a {} placeholder to greet yourself by name.

Next: Variables & Mutability