Why should you learn Rust?
Tags: rust, programming, beginners, webdev
Published: 2022-03-02 20:53:47

First of all if you think I'm talking about "crypto" here then this is not the place. I'm not a "crypto guy" and Rust is not made for blockchain stuff.


### Fast

Do you program in Python? Maybe JavaScript? but you need something which is faster and efficient. Well Rust is here, it compiles code fast and creates a single system binary.


### Memory Model

The fun part, Rust is memory safe unlike C/C++. Rust has a memory called "Ownership" each variable created in a function is owned by that function and if it passed onto another function (without the `&`) the ownership is transferred to that function. After the execution of that function (to one which we passed the variable) the "variable" is destroyed or no longer available to use again. To prevent transferring ownership and reusing that specific variable we use borrowing. Lets demonstrate using an real example.


```rs
fn print_value(value: i32) -> () {
    println!("{}", value)
}
fn main() {
    let age: i32 = 99;
    print_value(age);
    // error here because the ownership is transferred to `print_value`
    print_value(age)
}
```

Now with borrowing, first we need to tell the function that we are gonna borrow this `type`.
```rs
// &i32 -> in the function construction tells the function that this is going to be borrowed
fn print_value(value: &i32) -> () {
    println!("{}", value)
}
fn main() {
    let age: i32 = 99;
    print_value(&age);
    // no error because `main` function still has the ownership
    print_value(&age);
}
```
`print_value(&age)` means we are borrowing `&age`, alright now lets move forward.


### Type Safety

Rust is type safe too. 
![rust is awesome](https://media.giphy.com/media/3ohzdIuqJoo8QdKlnW/giphy.gif)
Means not all those stupid errors that JavaScript or Python gives you LOL.

You make `Data Structures` in Rust with the `struct` keyword.


```rs
struct User {
  age: i32,
  name: String
}

fn main() {
  // error here you cant put anything you want
  let user_1 = User { name: 12, age: "Haider".to_owned() };
  // ✔️ correct way to do
  let user_1 = User { name: "Haider".to_owned(), age: 99 }; 
}

```
 ### Is Rust high level or low level?

Rust is a high level language but also provides features of a low level language. Cool right? It also works on OS systems like Windows, Linux and mac.


### Operating System Support
Rust also works on Raspberry Pi and Arduino here are two videos which will explain how to run rust on them.

- [Rust Runs on Everything, Including the Raspberry Pi Pico | Adventures in Embedded Rust Programming
](https://youtu.be/Yi0WRF5WPFw)
- [Rust Runs on EVERYTHING, Including the Arduino | Adventures in Embedded Rust Programming](https://youtu.be/ZPSqhb4KKNc)




### Great package manager and build tool

Rust has a package manager called "cargo" and it is also its build tool. If you don't know what is a build tool, it simply compiles the code into binary runs and if you want you generate a production binary.

For production build:
```bash
$ cargo build —-release 
````

For running code (and which is also a development build)
```bash
$ cargo run
```
Let's about package management,like npm rust also has a website which lists all the `crates` (which simply means a package but in rust we call it a crate [crates.io](https://crates.io).



### Great for Web Development
I love Rust, its soo good for web development. Crates like `actix` and `rocket`(which is not maintained now).

You don't have worry about someone sending the wrong data, the type system will handle it, everything is fast (as I mentioned Rust is fast). I created some apis from which some are listed on my github and some private. Once you learn Rust you should try making api with it. In fact some developers made created crate with which you can make Frontend UIs with Rust, its called `Yew` [Yew's Docs](https://yew.rs/)


### Others things with Rust
Rust is also used for game development, embedded systems, writing interpreters and much more.

[AreWeYet?](https://wiki.mozilla.org/Areweyet)


### Resources to learn Rust

- [Zero To Mastery: Rust Programming: The Complete Developer's Guide](https://academy.zerotomastery.io/p/learn-rust)

- [Rust Book](https://doc.rust-lang.org/book/)



These are the two best resources in my opinion. 


Thank you if you read the whole blog post in return I want to you subscribe me on my Youtube and also drop me a follow on Twitter and Dev.to.

- [Youtube](https://www.youtube.com/channel/UCuP62HBRH9h5ROEP6ciCA4w)
- [Twitter](https://twitter.com/Haider_Ali__Dev)