To write the traditional Hello World program in Rust, create a text file called hello.rs
containing the following source code:
fn main() {
println!("Hello World!");
}
This defines a new function called main
, which takes no parameters and returns no data. This is where your program starts execution when run. Inside it, you have a println!
, which is a macro that prints text into the console.
To generate a binary application, invoke the Rust compiler by passing it the name of the source file:
$ rustc hello.rs
The resulting executable will have the same name as the main source module, so to run the program on a Linux or MacOS system, run:
$ ./hello Hello World!
On a Windows system, run:
C:\Rust> hello.exe Hello World!