Rust Tutorial 1: Writing Hello World
Tags: programming, rust
Published: 2023-09-14 15:05:34

_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/weu1kcc006j47db2oyxn.PNG)

---

## 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](https://www.rust-lang.org/tools/install) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsr0wxutn399w7h0lmjc.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qb0rmxgh2wny38uwgzo.PNG)

---

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/honi8kdk7gq2ife7cyc2.png)

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

![CodeLLDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fzfrivi9sxr5hf51hvwr.png)

---

### 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.

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

* Now, run the below command on your terminal to compile the Rust code.

```shell
rustc main.rs
```

* 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
```

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


![Hello World in Rust](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gy4eelmue9xxrb594u58.png)

---

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