Rust Hello World program
Rust Hello World program
Your first Rust program should produce a recognizable result. A console executable begins in fn main(). Braces enclose its body. Double quotes make a string literal, and println! writes a message followed by a newline. Names are case-sensitive, so Main and main have different meanings.
Save this program as main.rs. Compile with rustc --edition=2024 main.rs, then run ./main on macOS/Linux or .\main.exe on Windows. Rustc checks the source and produces machine instructions for the target. Statements normally end with semicolons; later we will see expressions that return a value without one.
What you’ll learn
- Explain main, println!, braces and string literals.
- Compile the greeting and predict its output order.
Rust Hello World program in action
Enter main
The first println! retains the original greeting.
Enter mainPrint Hello, World!
The second prints the school welcome on a separate line.
Print Hello, World!Print school welcome
The program then finishes.
Print school welcome
Compile and run Hello World
Save the complete source as main.rs in the Rust environment. No standard input is needed.
Rust 2024 · complete program
fn main() {
println!("Hello, World!");
println!("Welcome to River School");
}Expected output
Hello, World!
Welcome to River SchoolFollow the execution
The first println! retains the original greeting. The second prints the school welcome on a separate line. The program then finishes. Compilation produces an executable; it does not itself print these program messages.
Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.
Try a small variation
Your task: How would you keep only the school welcome?
Rust 2024 · complete program
fn main() {
println!("Hello, World!");
println!("Welcome to River School");
}Worked answer
Remove the first println! and keep the second. The executable will print Welcome to River School once after you rebuild it.
Mistakes and limits to recognize
A missing compiler, a source diagnostic and an executable launch failure are different problems. Read which stage failed before changing the program. The original output bookmarks remain available here.
Check your understanding
Take the idea into your next program
Remove the first println! and keep the second. The executable will print Welcome to River School once after you rebuild it. A missing compiler, a source diagnostic and an executable launch failure are different problems.
Further reading: the official Rust Book on rust hello world program.
Reaching the recap and completing the quizzes are separate milestones.