Rust from the beginning, your first program
Tags: rust, programming, beginners, tutorial
Published: 2022-06-06 22:21:11

---
title: Rust from the beginning, your first program
published: true
series : Rust series
description: In this first part on Rust we'll cover why Rust and how to build your first program.
tags: rust, programming, beginners, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a84iqfmybmqpm8jn4bob.jpg
---

> This is the first part in a series about the Rust programming language. You will learn to write, compile and run your first program in Rust.

{% embed https://www.youtube.com/watch?v=EPPSlmtbG2Q %}

## Resource

- [What is Rust, module](https://docs.microsoft.com/en-us/learn/modules/rust-introduction?wt.mc_id=academic-0000-chnoring)
- [Rust Docs](https://www.rust-lang.org/learn)

## Why Rust and where it shines

If you are considering Rust, you most likely have a few different applications in mind that requires speed and effective usage of memory like:

- **Game engines**, game engines sure demands both resources and speed. 
- **Websites and tools**, possibly more the tooling than the websites. 
- **Operating systems**, most operating systems at its core tend to be built-in high-performance languages.
- **Microcontrollers**. Close to the hardware.

The sales pitch that really sells Rust though is:

- **High speed and resource usage**. It combines best-in-class speed with a very low resource usage.
- **Nice approach to garbage collection and safety**. Rust solves problems associated with C/C++ such as garbage collection and safety.
- **Strong typing system means high-safety**. High safety through its strong type system.
- **Ergonomics**. Rust makes systems programming accessible by combining power with ergonomics.
- **Cargo for packages and managing code projects**. Great features like Cargo for managing projects.
- **Testing built-in**. Easy to test your code with no extra libraries.

Those all sounds good, but let's dive into it and learn to code in it and see what it has to offer.

## Install Rust

There're a few different ways to install Rust. The recommended way is to use [rustup](https://www.rust-lang.org/tools/install).

If you feel like evaluating the language first, check out the [playground](https://play.rust-lang.org/) that enables you to write code, compile and run it with no install.

## Exercise - Your first Rust program

Given that you've installed Rust, you will have access to the compiler `rustc`, an executable you use via the command line.

- Create a file _main.rs_
- Give it the following content:

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

- Compile program with `rustc`

   ```bash
   rustc main.rs
   ```

- Run program:

   ```bash
   ./main
   ```

   Here's the output:

   ```output
   Hello world
   ```

## The code line by line

It wasn't much code, but you now have a working application. So, what did you do? 

- **Entrypoint**, you defined an entry point to the application a method `main()`. This is you telling Rust where to start the program execution. You used the keyword `fn` to define a function, followed by the function name "main" and curly braces:

    ```rust
    fn main() {}
    ```

- **Printing to the console**. You used the print macro, `println!` and give it a string literal "Hello world".

   <pre>
   fn main()
   {
     <b>println!("Hello world");</b>
   }
   </pre>

That's it, that's all you needed for a program in Rust. Next, let's look at using variables.

## Variables and interpolation

You use variables in Rust to store values that you want to refer later to in code. There are different variable types you can work with, but for now, let's learn how to create a variable and use our `println!` macro.

You create a variable by typing:

```rust
let name = "Chris";
```

The above creates a variable `name` that you can refer later to in code.

You can now print `name` with the `println!()` macro like so:

```rust
println!("Hi {}", name);
```

The curly braces `{}` interpolates your variable `name` and you end up with "Hi Chris" where you to compile and run the code.

Let's actually do that next.

## Exercise - modify your code

Now that you learned about defining a variable and printing it, lets modify your existing code. 

1. Change _app.rs_ to this code:

    ```rust
    fn main() {
      let name = "Chris";
      println!("Hi {}", name);
    }
    ```

1. Compile the program with `rustc`:

   ```bash
   rustc main.rs
   ```

1. Run the program:

   ```bash
   ./main # it's an exe file on windows
   ```

   You now see "Hello Chris"

Congratulations, you've now started your journey to become a programmer in Rust, or as it's also called, a _Rustacean_.

## Summary

You learned about Rust, why and where to use it. Additionally, you've created a program in it and you're now ready to learn more about Rust. Welcome Rustacean :)