DEV Community

Cover image for Why should you learn Rust?
Haider Ali
Haider Ali

Posted on • Edited on

Why should you learn Rust?

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.

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)
}
Enter fullscreen mode Exit fullscreen mode

Now with borrowing, first we need to tell the function that we are gonna borrow this type.

// &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);
}
Enter fullscreen mode Exit fullscreen mode

print_value(&age) means we are borrowing &age, alright now lets move forward.

Type Safety

Rust is type safe too.
rust is awesome
Means not all those stupid errors that JavaScript or Python gives you LOL.

You make Data Structures in Rust with the struct keyword.

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 }; 
}

Enter fullscreen mode Exit fullscreen mode

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

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:

$ cargo build —-release 
Enter fullscreen mode Exit fullscreen mode


`

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.

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

Others things with Rust

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

AreWeYet?

Resources to learn Rust

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.

Top comments (0)