Basic

Your First Rust Program

Print text to the screen and understand the minimal structure of a Rust program.

What you will see

COMPILE_ERROR

About 2 min of reading

Try It

You will write a small Rust program, print text to the screen, and see how the main function and println! macro work together.

1

I create a program with just a main function and one println! call, then run it. The text between the quotes prints followed by a newline.

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

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Hello, World!
2

I add a second println! call. The two statements run top to bottom, so each prints on its own line.

fn main() { println!("first"); println!("second"); }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
first second
3

I use the {} placeholder to substitute values into the string. println! formats each argument in order.

fn main() { let name = "Ada"; let age = 30; println!("{} is {}", name, age); }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada is 30
4

I build the source with rustc and run the binary. Building first produces a native executable I can then run directly.

fn main() { println!("built"); }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
BUILDS_OK
On Windows the compiled binary is main.exe, not main.
5

I pass the wrong number of arguments to format!. The compiler rejects the mismatch, showing each placeholder needs a matching value.

fn main() { let x = 5; println!("{} {}", x); }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
COMPILE_ERROR
Every {} placeholder needs a matching argument.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

The main function runs top to bottom; println! formats a string and sends it to the terminal.
1 rustc compiles main.rs into a native binary
↓
2 The OS loads the binary and calls main()
↓
3 println! formats the literal string
↓
4 The formatted text is written to standard output
Why this works
The main function

Every executable Rust program has one fn main(). The runtime calls it first:

fn main() {
    println!("first");
    println!("second");
}

Output:

first
second

Statements do not need semicolons at the end of a line, but expression tails do, this is a common beginner gotcha.

When you have a choice

Macros vs functions

println! is a macro, not a function. Macros are written with a ! and expand at compile time:

println!("{}", 42);   // expands to formatting code
let x = 1;            // no semicolon needed here

The trailing ; on let x = 1; is a statement, while let y = x without ; is an expression whose value flows into the next line.

Examples

Hello World with println!

Printing to the screen

Rust prints text with the println! macro. Every runnable program starts in fn main().

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

fn main() is the entry point. println! is a macro (note the !), not a function, it expands to formatting code.

Why it works: the macro writes the string plus a newline to standard output.

Running a program

Building and running

Save the code as main.rs, then run:

rustc main.rs
./main

rustc compiles the source to a native binary. Rust is a compiled language, you build once, then run the executable.

Watch out: on Windows the binary is main.exe, not main.

Comments and structure

Annotating code

// single-line comment

/* multi-line
   comment */

Comments are ignored by the compiler but help humans. // is for inline notes; / / is for longer blocks. Rust also has doc comments /// for generating documentation.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Variables and Immutability
Variables and Immutability →