Hello World in Rust

Hello World in Rust

Introduction

Rust is a Systems Programming language that has been gaining more and more traction recently. Many Developers see it as a safer alternative of C, C++.

And so let's not waste any time and get started with writing our first Rust program.

Hello World

First things first, you have to install Rust.

Hope that's installed. Now you have to create a directory. Inside the directory create a file named main.rs

All Rust files end with the extension .rs

Now, write the below program.

fn main() {
    println!("Hello World");
}

To compile the program into an executable. Run the below command in the terminal in the same directory as the main.rs file. This is very similar to C, C++

rustc main.rs

Now you'll find a main.exe file if you're using Windows. Else you'll find main

Simply type main.exe (or main) in the terminal. It will print Hello World.

That's it. See you again in another informative article!

Just kidding! Let's analyse which part does what, to have an in-depth understanding!

Analysis

If you're already familiar with other Programming languages, you might already understand that there is not much difference here.

fn main() {
    println!("Hello World");
}

fn is the keyword used to declare functions in Rust. What're functions? We'll explore in a later article!

In this case main is the name of the function. You can write functions having other names also, but every Rust file should contain a main function, where the program execution starts. Now the main function, may also contain some arguments, and that should come inside the parentheses () In our case there is none.

Then we have the contents of our function inside the curly braces {}

println!("Hello World");

We're writing println! println can simply be considered as print line. The ! in Rust calls a macro, which we will discuss later.

We're then politely asking Rust to print the following line "Hello World" which should always be written inside double quotes ""

Lastly the semicolon ; says the expression is over and you can move on to the next line (if there is)

Conclusion

That's all about our very first very simple Rust program.

We'll discuss more about Rust, so see you again in another very exciting article!