Rust Basics
Hello World in Rust: Structure of a Program
Write your first Rust program and see what each part does: fn main, the braces and println!.
What you'll learn
- Where a Rust program starts
- Printing with println!
- Why println! ends with !
From source to output
- 1
main.rsYou write the code - 2
rustcThe compiler checks and builds it - 3
main()The program starts in main - 4
Hello, Rust!The text appears
How a Rust program runs
Every Rust program starts in a function called main. The println! macro prints a line of text; the exclamation mark shows that it is a macro, not an ordinary function.
fn declares a function and the braces hold its body. Each statement ends with a semicolon. The compiler turns the file into a program, then the program runs main and prints the text.
Key points
- Every program starts in fn main.
- println! is a macro: the ! tells you so.
- rustc compiles the file into a program before it runs.
Runnable Rust example
Rust
main.rs
fn main() {
println!("Hello, Rust!");
}
Hello, Rust!Practice
Open the example in the online compiler, change a value or add a line, and predict the output before you run it. Then break the code on purpose and read the compiler's message.
Finished this lesson?
Mark it as learned to track your progress.
Your progress is saved in this browser Sign in to keep it on every device.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Odd, Even, or Neither Classify a number by whether it is an odd integer, an even integer, or neither. Easy
- Square a Number Multiply an integer by itself. Easy