Rust: Initial thoughts
Tags: rust, devjournal, security, discuss
Published: 2021-01-12 04:52:08

Recently, I have started to learn Rust. Why Rust you ask? Well, it's one of the best programming languages in each of these categories: performance, reliability, and productivity which we will discuss later. Also, coming from a Python/Javascript background, I thought it would be a challenge to learn a low level, statically typed language. Let's dive right into my initial thoughts on Rust and how it compares to other languages!

****

## Learning Rust

Before I started learning Rust, I thought that it would be hard to get started and I was even wondering if I would be able to find good resources to learn Rust. I was wrong, big time. There are countless resources for learning Rust. I decided to take a <a href="https://www.udemy.com/course/rust-fundamentals/">Udemy Course</a> and follow the <a href="https://doc.rust-lang.org/book/">offical Rust Programming Language book</a>. The Udemy course and book were awesome! I would highly recommend looking at both when you're learning Rust. After that, I decided to play around with Rust, creating a <a href="https://github.com/henryboisdequin/rust-calculator">simple command-line calculator application</a>. To conclude, <del>learning</del> getting started with Rust is as simple as <del>learning</del> getting started with Python.

****

## Rust is hard to learn 

Rust is hard to learn because its a low-level programming language. If a programming language is considered a low level, it means that the language provides little difference from a computer's set architecture or in simpler terms it's closer to machine code (basically 0s and 1s). Especially coming from a high-level programming language background, Rust was not something I was used to. Learning about lifetimes, borrowing, and more (memory management) was something I didn't even have to look at in Javascript or Python. But being a low-level programming language comes with its perks.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gqwxha8ohrdef5hwieem.png)

****

## Rust is extremely powerful

There is a reason why some programming languages (particularly targetted towards speed) are low level. To put it simply: since low-level languages are closer to machine code it's faster for them to compile to machine code and get executed then higher-level programming languages. Aside from being low-level, Rust also has some more very powerful features which make it memory safe, fast, and a productive environment. Let's have a look at these features.

### Rust is fast

Rust is fast for two main reasons: it's statically typed and there is little to no overhead (garbage collection). I will get into garbage collection in a little bit but first I want to talk about how Rust is statically typed and compiled. Being statically typed and complied means that a type of a variable is known at compile time (before running your code). This means that instead of figuring out the type of your variable at run time, all Rust needs to do is execute your code. This is one less step needed at run time which makes Rust that much faster than other dynamically typed languages like Python or Javascript. Let's have a look at an example:

```rust
fn main() {
  let message: &str = "Hello World";
  println!("{}", message);
}
```

As you can see, in the main function, we are defining a variable which has a type of `&str`. Instead of trying to figure out the type at compile-time, Rust already knows that our message is of type `&str`. Let's have a look at the equivalent in Python:

```python
def main():
  message = "Hello World"
  print(message)

main()
```

As you can see, we don't provide a type for our message variable hence the variable type is unknown until the program runs.

### No garbage collection

Next, I want to talk about garbage collection. Most languages which are memory safe use a garbage collector to achieve this. What is a garbage collector? A garbage collector attempts to reclaim memory occupied by objects which are no longer used by the program. This prevents memory leaks and memory issues. This is how languages like Python, Javascript, or Java achieve memory safety. It's not all pros with a garbage collector. Having a garbage collector in your programming language makes it slow. Why is it slow you ask? For a garbage collector to collect its garbage, it stops the program, does its tasks, and then the program continues. As you can see, it stops to ensure memory safety which is why it is slow. Instead of implementing a garbage collector, Rust implements a concept called ownership. There are <a href="https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html">3 rules</a> to ownership in Rust:

1. Each value in Rust has a variable that’s called its owner.
2. There can only be one owner at a time.
3. When the owner goes out of scope, the value will be dropped.

If one of these rules is not followed at any given time, the program will stop which eliminates all memory safety issues. Let's have a look at an example:

```rust
fn main() {
  let var = 1;
  {
    let var1 = var; 
  }
  println!("Var: {}, Var 1: {}", var, var1);
}
```

In this example, we are defining a variable called `var` and variable called `var1`. Since var1 is defined in another scope and its value is var, var is no longer valid (rule 3), hence this program results in an error.

### The compiler is my best friend

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1zxxkwqsp62xp8o4v0gz.png)

The Rust compiler output is beautiful. It doesn't only tell you what the problem is, it tells you how to fix it. No more sleepless nights waiting for a response on StackOverflow, just implement what the compiler tells you what to do and no more bugs.

****

## Should you learn Rust?

Rust is definitely something worth looking at. Even if you don't plan to use Rust for your future projects, for any developer coming from a higher-level programming language background to have a look at Rust. With that said, now I will be focusing on backend web development with Rust. Who knows, it might replace my Node.js backend (I'll keep you posted). 

****

In conclusion, my experience thus far with Rust has been awesome. I would highly recommend playing around with Rust. Thanks for reading.

**Henry**