Rust Tutorial 1: Writing Hello World

khair_al_anam

Khair Alanam

Posted on September 14, 2023

Rust Tutorial 1: Writing Hello World

Reading time: 3 minutes

Hey everyone! Recently I started learning the Rust programming language for a personal side project of mine. But there aren't many resources available apart from the official documentation. So I decided I might as well share the stuff I have learned in Rust.

So without further ado, let's learn Rust!

Rust in Google Images


How is Rust different?

Rust is a low-level programming language like C. It's a compiled language like C. But unlike C, Rust is memory-safe (No segfaults nightmares). And unlike many languages, Rust doesn't use a garbage collector. This "best of both worlds"-feature makes Rust insanely fast and efficient.

The main factor that makes it different is its memory management. I will cover this in a later tutorial. But to give you a brief idea, Rust language works on ownership and borrowing concepts of memory. In other words, when data are being passed on to different variables, the data don't get copied to all these variables. Instead, the one that currently has the data will have ownership of it while the other variables "lose" ownership of it.

Now let's get to the interesting part.


Writing your first program in Rust

Before writing your first program in Rust, let's install Rust!


Installing Rust

The installation differs for each platform. But I will be installing Rust for Windows.

  • Go to this link to install Rust for Windows.

  • Click on the button that downloads the rustup installer. Mine is Windows 10 64-bit. So I will download a 64-bit installer.

  • Go to the folder where you downloaded the installer and open it.

You will get the below screen:

rustup installer screen

  • Type "1" and then press Enter. Wait until everything is installed. Also, Rust requires Visual Studio C++ Build Tools, which will be prompted by the installer for you to install these tools.

Once everything is done, you will get the below image which implies Rust is finally installed. Press Enter.

Rust is installed


Setup Rust in VS Code

After the installation, create a new folder and open it in VS Code.

  • Go to Extensions. Search for "rust-analyzer" and install it.

Rust analyzer

  • Also, search for "CodeLLDB" and install it.

CodeLLDB


Writing Rust code

Every program in Rust ends with the file extension .rs.

  • Create a file and name it main.rs.

  • Like C, C++, and Java, Rust runs your main program inside a main function.

  • Now, write your program.



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


Enter fullscreen mode Exit fullscreen mode
  • Now, run the below command on your terminal to compile the Rust code.


rustc main.rs


Enter fullscreen mode Exit fullscreen mode
  • After running this command, you will see some files including an executable file.

  • Now finally, run the below command to see the output.


 shell
./main


Enter fullscreen mode Exit fullscreen mode

This will run the main.exe file and print "Hello World" in your terminal.

Hello World in Rust


Great job! You have written in your first program in Rust!

Plus, you have also learned how to print texts in the terminal using println!() as well as initializing a function using the fn keyword.

In the next tutorial, we will be using Rust's in-built package manager called Cargo to make simple Rust programs and build Rust projects.

Have a great day!

GitHub Repo: https://github.com/khairalanam/rust-tutorial

If you like whatever I write here, follow me on Devto and check out my socials:

LinkedIn: https://www.linkedin.com/in/khair-alanam-b27b69221/
Twitter: https://www.twitter.com/khair_alanam
GitHub: https://github.com/khairalanam

💖 💪 🙅 🚩
khair_al_anam
Khair Alanam

Posted on September 14, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related