Rust Your First Program Hello World
Writing a Hello World program is the first step every programmer takes when learning a new language. In Rust, this program teaches you the basic structure that every Rust program follows.
Open Your Project
If you created the hello_rust project in the previous topic, open the file src/main.rs in your editor. It already contains a Hello World program that Cargo wrote for you.
The Hello World Code
fn main() {
println!("Hello, World!");
}
Those four lines are a complete Rust program. When you run it, the program prints the text Hello, World! to your screen and then stops.
Breaking Down Each Part
fn main()
fn is short for "function." A function is a named block of code that does something. main is a special name. Every Rust program must have a function called main. This is where the program starts running. The two parentheses () hold any inputs the function receives — in this case, none.
The Curly Braces { }
The opening brace { marks where the function body begins. The closing brace } marks where it ends. Everything between these braces is the code that runs when the function is called.
println!
println! prints a line of text to the screen. The exclamation mark at the end tells you this is a macro, not a regular function. A macro is a shortcut that expands into more complex code when compiled. You do not need to understand macros deeply right now — just know that println! prints text and adds a new line at the end.
The Semicolon
The semicolon ; at the end of the println! line marks the end of that statement. In Rust, most statements end with a semicolon. Forgetting one is a common mistake that the compiler will point out with a clear error message.
The Road Sign Diagram
fn main() { ← Road starts here (program entry point)
println!("Hello!"); ← Do this action (print text)
← Semicolon = end of this action
} ← Road ends here
Running the Program
Open your terminal, navigate to your project folder, and run:
cargo run
Cargo compiles your code and runs it. You will see output like this:
Compiling hello_rust v0.1.0 (/your/path/hello_rust)
Finished dev [unoptimized + debuginfo] target(s) in 1.23s
Running `target/debug/hello_rust`
Hello, World!
The last line — Hello, World! — is the output of your program. Everything above it is information from Cargo about what it did.
What Happens During Compilation
When you run cargo run, two things happen in order.
Step 1: Compilation
The Rust compiler (rustc) reads your main.rs file and checks it for errors. If no errors exist, it converts your code into a binary file — a file the computer can run directly. This binary is stored in the target/debug/ folder inside your project.
Step 2: Execution
Cargo runs the binary file it just created. The program executes from the first line of main() to the last and then exits.
The Factory Diagram
[ main.rs ] → [ Rust Compiler ] → [ Binary file ] → [ Screen output ] (your code) (checks + builds) (ready to run) Hello, World!
Printing Different Text
Change the text inside the quotes to print anything you want:
fn main() {
println!("I am learning Rust.");
println!("This is my second line.");
}
Run cargo run again. The program now prints two lines:
I am learning Rust. This is my second line.
Each println! call prints one line. You can have as many of them as you need inside main().
Printing Values Inside Text
You can insert values into the printed text using curly braces as placeholders:
fn main() {
println!("The answer is {}.", 42);
}
Output:
The answer is 42.
The {} inside the text is a placeholder. Rust replaces it with the value you provide after the comma. This is called string formatting.
The Fill-in-the-Blank Diagram
println!("My name is {} and I am {} years old.", "Alex", 25);
↑ ↑
"Alex" 25
Output: My name is Alex and I am 25 years old.
Common Beginner Mistakes
Mistake 1: Missing the Exclamation Mark
println("Hello"); ← Wrong: missing !
println!("Hello"); ← Correct
Mistake 2: Missing the Semicolon
println!("Hello") ← Wrong: missing ;
println!("Hello"); ← Correct
Mistake 3: Wrong Quote Style
println!('Hello'); ← Wrong: single quotes are for single characters
println!("Hello"); ← Correct: use double quotes for text
What You Just Learned
fn main() → Entry point of every Rust program
println! → Prints text to the screen with a new line
{ } → Groups code inside a function
; → Ends a statement
{} → Placeholder for values inside printed text
cargo run → Compiles and runs your program
