Rust from Zero Module 1: Hello, Rust Lesson 1.1 10 min

Your first Rust program (and why Rust is worth learning)

Run a real Rust program in the next 60 seconds, then learn what every line of it means.

Press the orange Run button below. That’s it. You just compiled and ran a Rust program.

main.rs Open in Playground
fn main() {
    println!("Hello, world!");
}
Expected output
Hello, world!

No installing, no setup. The code ran on the official Rust Playground and came back with what it printed. Every code box in this course works the same way, and you can edit any of them. Try changing world to your own name and press Run again.

What is Rust, in one minute

Rust is a programming language for building fast, reliable software. It is used for command-line tools, web servers, game engines, browsers and even operating systems. Parts of Firefox, Android, Windows and the Linux kernel are written in Rust.

Rust has a reputation for being hard. Here is the honest version: Rust is strict, not hard. It checks your program very carefully before it runs, and when something is wrong it tells you exactly what and where, often with the fix. That strictness is why Rust programs crash so rarely. In this course we treat the compiler as a patient teacher, not an enemy.

Your code goes to the compiler, which checks it and builds a program; the program runs and prints output

That picture is the whole story of every program in this course:

  1. You write source code in a file, by convention main.rs.
  2. The compiler (rustc) reads it, checks it, and either builds a program or explains what to fix.
  3. The program runs and prints its output.

Every line of Hello, world

Here is the program again, taken apart.

The parts of hello world: fn main, the curly braces, the println! macro, the text in quotes and the semicolon

  • fn main() declares a function called main. fn is short for function. Every Rust program starts running at main.
  • { } The curly braces hold the function’s body: the steps to run, in order.
  • println! prints a line of text. The ! matters: it marks a macro, a special kind of command that writes code for you. You will meet more macros later; for now, remember that printing is println! with the !.
  • "Hello, world!" is a string, text in double quotes.
  • ; The semicolon ends the step, like a full stop ends a sentence.

Predict, then check

Learning sticks best when you guess first. Before you look, what will this print?

Predict the output

What will this program print?

fn main() {
    println!("Rust is fun!");
    println!("Let's begin.");
    println!("Ready?");
}
What will this program print?
Show the real output
Rust is fun!
Let's begin.
Ready?

Each println! prints its text and then moves to a new line, so the three lines appear one after another, top to bottom, in the order they are written.

Your turn: print three lines about you

Change the program so it prints three lines: your name, where you are, and one thing you want to build with Rust. Press Run to check. There is no wrong answer here; if it runs, you did it.

about_me.rs Open in Playground
fn main() {
    println!("My name is Sam.");
    println!("I live in Chennai.");
    println!("I want to build a game.");
}
Expected output
My name is Sam.
I live in Chennai.
I want to build a game.

Fix the bug

Mistakes are part of programming, and Rust’s error messages are some of the friendliest around. This program has one small mistake. Read the compiler’s message, then fix the code and press Run until it works.

Fix the bug

This program does not compile. Read the error, change the code, and run it until it works.

main.rs
fn main() {
    println("Hello, Rust!");
}

What the compiler says

error[E0423]: cannot find function `println` in this scope
 --> src/main.rs:2:5
  |
2 |     println("Hello, Rust!");
  |     ^^^^^^^ not found in this scope
  |
  = note: a macro named `println` exists in another namespace
help: use `!` to invoke the macro
  |
2 |     println!("Hello, Rust!");
  |            +
Hint

The compiler cannot find a function called println, but it notices a macro with that name. Read its "help" line: what does a macro name need at the end?

Show the fix
fn main() {
    println!("Hello, Rust!");
}

It prints

Hello, Rust!

Notice what the compiler did there: it pointed at the exact spot, named the problem, and suggested the fix. Get used to reading these messages top to bottom. They are the fastest way to learn.

Quick check

Quick check

  1. Where does every Rust program start running?
  2. Why does println! have an exclamation mark?
  3. What ends each step inside a function?

Recap

  • A Rust program starts at fn main(), and its steps go inside { }.
  • println!("...") prints a line. The ! means it is a macro.
  • The compiler checks your code before it runs. When it complains, read the message: it usually tells you the fix.

Next: printing is more useful when it can show values, not just fixed text. In the next lesson you will make println! fill in blanks.

Leave a Reply

Your email address will not be published. Required fields are marked *