#06　What is Rust lang?

## Introduction

Do you know which programming language has been rated the most "loved" among developers for beyond five years? It's [Rust][1]!! The Rust programming language first debuted in 2010. Even though it is a fairly young language compared to C++, and C lang, the language has gained much attention from many developers. Rust is a systems programming language that is designed to be safe, concurrent, and fast. It is also one of the famous [open-source][2] programming languages that are maintained by hundreds of contributors, most of them are volunteers. But why do a lot of developers love and choose Rust? Because there are some reasons.

Rust is

1.  High Performance
2.  Reliable
3.  High Productivity

Rust is used by many companies such as

- Microsoft
- Amazon
- Discord ([switch Golang -> Rust to speed up its system][3])
- Dropbox (to sync files to your computer)
- Cloudflare (to process more than 20% of all internet traffic)
- ...

Rust will be supported in the **Linux kernel**.

Because of such reasons, Rust has been nominated as one of the "love" languages among developers all over the world.

So you want to know more about Rust language? Let's dig into the Rust world.

## Background

Firstly, I will explain a little bit of the history of Rust programming language. Rust began as a side project of [Graydon Hoare][4] who is a [Mozilla][5] Research employee. Mozilla is an open-source browser company. One day, he went back to his apartment, the elevator was out of order; its software had crashed. He got annoyed while he climbed the stairs. He knew the reason why the software crashed. This was because of memory management. The software inside devices like elevators is often written in languages like C++ or C, which are famous for allowing programmers to write code that runs very fast. However, those language cause memory bugs quite often. According to Microsoft, 70% of the vulnerabilities in its code are due to memory errors from code written in these languages. Then he decided to develop a new computer language that is focused on memory safety and is even able to write small, fast code.

## Rust's unique features

Rust has some very unique features compared to other languages. For example,

### Does not have garbage collection

Current popular programming languages like Java, JavaScript, and Python use garbage collectors. What are garbage collectors? It automatically manages the computer memory to relieve stress on coders. It would periodically clean up the memory as a piece of software was running. But this is not good because garbage collection takes up the crucial processing time. It means these languages used much more memory. Then, Rust took a different way. It would not require programmers to manually figure out where in memory they were putting data. Rust does that.

### Ownership

It enables Rust to make memory safety guarantees without needing a garbage collector. Ownership is a set of rules that govern how a Rust program manages memory. Memory is managed through a system of ownership with a set of rules that the compiler checks. If any of the rules are violated, the program won't compile.

This is one of example why Rust will not compile because of the ownership problem.

```rust
let s1 = String::from("hello");
let s2 = s1;

println!("{}, world!", s1);
// This is because variable s1 has already moved to variable s2
// so can not use it anymore after moving.
```

### Borrowing

If you want to use a value in a function, without transferring ownership to the function, you can borrow the value temporarily from its owner. When the function is done with the value, it's given back to the owner.

Let's see one example!

```rust
fn main() {
    let a = String::from("Hello");
    // reference a with &
    let str_len = get_str_length(&a);
    println!("String length: {}", str_len);
}

fn get_str_length(s:&String) -> usize {
    s.len()
}
```

### Option and Result

Rust has unique enum types Option and Result.

Option type can either be something or nothing.

```rust
enum Option<T> {
    None,
    // T is the generic type
    Some(T),
}
```

Options are commonly paired with a pattern matching to query the presence of a value and take action, always accounting for the None case.

Similar to Option above, there is Result type. This is used for returning and propagating errors. It can be one of two things: it's either an Ok or Error. Ok represent success and contains a value, and Err(E) represents error and contains an error value.

```rust
enum Result<T, E> {
    Ok(T),
    Err(E),
}
```

That's all for a brief introduction. If you don't understand some code or any features, don't worry. It's just started. Next, let's install Rust.


## Welcome to Rust

Before writing code in Rust, we need to set up development environment. If you want to try writing code without installing, there is an editor in browser, [here][5].

I would recommend [wsl][6] for windows user when you set up new development environment. Otherwise, you can come here to download.

1.  Install Rust

```terminal
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# info: installing component 'rustfmt'
# info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

#  stable-x86_64-unknown-linux-gnu installed - rustc 1.68.0 (2c8cc3432 2023-03-06)


# Rust is installed now. Great!

# To get started you may need to restart your current shell.
# This would reload your PATH environment variable to include
# Cargo's bin directory ($HOME/.cargo/bin).
```

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

2.  To configure your current shell, run this command or close terminal then open again!

```terminal
source $HOME/.cargo/env
```

3.  Check that you installed Rust correctly

```terminal
cargo --version

# cargo 1.68.0 (115f34552 2023-02-26)
# cargo build system and package manager is similar to npm in JavaScript.
```

Congulatulations! You finally ready to go rust development.

## Hello world

Let's build first small app which is called _Hello World_ app. This will print Hello, world! text.

```terminal
cargo new hello-world

# Created binary (application) `hello-world` package
```

```terminal
cd hello-world
```

Open your favorite editor. In my case, I mostly use Visual Studio Code. If you use VS Code, I recommend to install this extension.

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

Run app

```terminal
cargo run

# Running `target/debug/hello-world`
# Hello, world!
```

Congulatulations! You did it. :clap:

## Additional

If you want to dig in more, I can recommend some youtube channels, and books

### Blog
- [Amos's blog](https://fasterthanli.me/)
- [Mara's blog](https://blog.m-ou.se/)
- [Yaah's blog](https://yaah.dev/)

### Recommend Channel
- [jonhoo][7]
- [fasterthanlime](https://youtube.com/@fasterthanlime)

### Recommend books
- [The Rust Programming Language][8]
- [Welcome to Comprehensive Rust][9]

Thank you for your time. Enjoy your Rust life!

## References

- [How Rust went from a side project to the world窶冱 most-loved programming language][10]

[1]: https://www.rust-lang.org/
[2]: https://github.com/rust-lang/rust
[3]: https://discord.com/blog/why-discord-is-switching-from-go-to-rust
[4]: https://github.com/graydon
[5]: https://www.mozilla.org/
[6]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021
[7]: https://www.youtube.com/@jonhoo
[8]: https://doc.rust-lang.org/book/title-page.html
[9]: https://google.github.io/comprehensive-rust/
[10]: https://www.technologyreview.com/2023/02/14/1067869/rust-worlds-fastest-growing-programming-language/