Rust Hello World Program
Writing Your First Rust Program
Create a file named main.rs
. In Rust, the default entry file is typically main.rs
for binary applications.
fn main() {
println!("Hello, World!");
}
Hello, World!
Compiling the Program
Open a terminal where your main.rs
file is saved and run the following command using rustc
:
rustc main.rs
What happens during compilation?
The rustc
command invokes the Rust compiler. Here's what it does:
- Parses your source file and checks for syntax and type correctness.
- Compiles the code into machine-level instructions.
- Generates an executable file, typically named
main
by default.
The output executable is ready to run on your system.
Running the Program
To run the compiled program, use the following command in your terminal:
./main
What happens during execution?
When the program runs:
- It loads the binary into memory.
- Executes from the
main
function. - Prints
Hello, World!
to the terminal.
Hello, World!
You’ve now successfully written, compiled, and executed your first Rust program — a solid start to your Rust journey!
Common Issues to Watch For
- Missing semicolons: Each statement in Rust usually ends with
;
. - Syntax errors: Forgetting parentheses in
println!
or mistyping the macro. - File name mismatch: Ensure your file is named correctly and you're in the right directory.
Comments
Loading comments...