Hello, World!
You’ve installed Rust. Now let’s make it do something.
The time-honoured tradition in every programming language is to start with a program that prints Hello, world! to the screen. Rust is no different — and along the way you’ll learn how the Rust compiler, source files, and executables all fit together.
This chapter assumes basic comfort with a terminal / command prompt. You can use any text editor or IDE you like — Rust has no opinions about that.
Project Setup
Create a projects directory
It’s good practice to keep all your Rust projects in one place.
Linux / macOS / PowerShell
mkdir ~/projects
cd ~/projects
mkdir hello_world
cd hello_worldCreate the source file
Inside hello_world/, create a file named main.rs.
Rust file conventions:
- All Rust source files end in
.rs - Use
snake_casefor multi-word filenames — e.g.hello_world.rs, notHelloWorld.rs
Your project should now look like this:
- main.rs
Write the Program
Open main.rs in your editor and type the following:
fn main() {
println!("Hello, world!");
}Save the file. That’s your entire first Rust program — two lines of actual logic.
Compile & Run
Linux / macOS
rustc main.rs
./mainYou should see:
Hello, world!If you do — congratulations, you are officially a Rust programmer. 🎉
After compiling, your directory contains two new files:
- main.rs
- main ← binary (Linux/macOS)
- main.exe ← binary (Windows)
- main.pdb ← debug info (Windows only)
If you see command not found: rustc, Rust isn’t on your PATH yet. Go back to the Installation page and re-source your shell environment.
Anatomy of the Program
Let’s dissect every part of those two lines.
fn main() — the entry point
fn main() {
}| Part | Meaning |
|---|---|
fn | Keyword that declares a new function |
main | The function name — special: always the first code that runs in an executable |
() | Parameter list — empty here, meaning no inputs |
{ } | Function body — all logic goes inside these curly brackets |
Style tip: Rust convention places the opening { on the same line as the function signature, separated by a single space. The formatter rustfmt enforces this automatically — run rustfmt main.rs to auto-fix style.
println! — a macro, not a function
println!("Hello, world!");Three things to notice on this single line:
1. The ! means it’s a macro
println!("Hello, world!"); // ← macro (note the !)
println("Hello, world!"); // ← this would be a function call (doesn't exist)In Rust, a ! suffix on a call means you’re invoking a macro — code that generates code at compile time. println! expands into more complex output logic under the hood. Macros don’t always follow the same rules as regular functions.
You don’t need to understand macros deeply right now. Just remember:
!= macro.
2. The string argument
"Hello, world!" is a string literal passed to println!. It gets printed to stdout followed by a newline character.
3. The semicolon ends the statement
println!("Hello, world!");
// ^ semicolonIn Rust, most statements end with a ;. It signals to the compiler that this expression is complete and the next one can begin.
Missing a semicolon is one of the most common early mistakes. The Rust compiler will tell you exactly where one is needed — its error messages are famously helpful.
How Rust Compilation Works
You ran two separate commands: rustc main.rs then ./main. That’s intentional — compilation and execution are separate steps in Rust.
Rust is an ahead-of-time compiled language. This means:
- You compile once → get a standalone binary
- That binary runs on any compatible machine without Rust installed
- Compare this to Python or Ruby, where the interpreter must be present to run a
.pyor.rbfile
C / C++ background? This will feel familiar — rustc works similarly to gcc or clang. The key difference is that Rust’s compiler produces significantly more helpful error messages.
When to use rustc vs cargo
For everything beyond “Hello, world!”, you’ll use Cargo — Rust’s build system and package manager. That’s exactly what the next chapter covers.
Quick Reference
fn main() { // entry point — runs first
println!("Hello, world!"); // macro call — prints + newline
} // end of function bodyrustc main.rs # compile → produces ./main binary
./main # run the binaryWhat’s Next?
You’ve compiled and run your first Rust program and understand what each line does. Next up: Cargo — Rust’s official build tool and package manager that makes real-world projects manageable.