The journey to learning any programming language starts with a simple “Hello World” program. To create a “Hello World” program in Rust, follow these steps:
main.rs – rust files always end with the .rs extension.main.rs file and write the following code in it:fn main() {
println!("Hello world!");
}
$ rustc main.rs
$ ./main
> rustc main.rs
> .\main.exe
The following is an executable “Hello World” program in Rust:
fn main() {println!("Hello World!");}
The code fn main() {} defines a function named main. The main function is special because it is always the first code that runs in a Rust executable program. Any arguments will go inside the parenthesis () and the code will go inside the curly brackets {}.
The code println!("Hello, world!"); simply prints the string inside the parenthesis onto the screen. Note that println! has an exclamation mark at the end of it – this means that it is a macro and not a function. While functions generate a function call, macros are expanded into a source code that is compiled with the rest of the program.
Free Resources