Rust's overview from Javascript / Typescript

I have been actively learning Rust from the Rust book for the last 7 days averaging around 5 hours per day. I am currently on chapter 19 which discusses some "advanced features". Here is my thought as a Javascript developer.

The Rust programming language

Rust is a low-level programming language just like c or c++. However, what makes it unique is its promise to "guarantee memory-safety and thread-safety" while maintaining performances. In fact, benchmarks often present Rust as being as fast as c++. You can learn more about the Rust programming language on their official website.

Overview

Rust feels modern and comes with a lot of utility functions that make development easier. It has a great community and detailed documentation. Rust also has great IDE support and a very friendly error/warning message.

Some similarities

  • Array methods

    Rust has built-in utility functions like .filter or .map to iterate over an "iterators." Their syntax is quite similar to their javascript conterpart. Bellow is an example of rust .map which increment all element of an array by 1.

    let array: Vec<i32> = [1, 2, 3].iter().map(|x|  x  +  1).collect();
    
  • Variable declaration

    Rust variable declaration syntax as you may have noticed in the above example is similar to the one used in typescripts; They use the let keywords. The type definition is not always required as the compiler can often make good guesses from its usage. The following is a perfectly valid rust code.

    let num = 32;
    
  • Closures

    Rust has closures. They are very similar to javascript lambdas. Bellow is an example of javascript closure;

    let  my_closure  =  |x:  i32, y:  i32| {x  +  y};
    println!("The result {}", my_closure(12,12));
    
    // prints "The result 24
    

Some differences

  • Imutability is the default

    All variable in rust are immutable by default;

    let num1 = 4;
    // num1 is considered as a constant variable
    let mut num2 = 4;
    // num2 is mutable;
    

    One thing to note though about rust immutability is that it is recursive and behaves like "Object.freeze" in javascript or ReadOnly type in typescript. For example, pushing to an non-mutable variable raises a compile error.

    // this does not compile
    let non_mutable_array  =  vec![12,25,58];
    non_mutable_array.push(4);
    
  • No garbage collection

    Rust is not garbage collected and guarantees memory safety through the concept of ownerships whose rules are that :

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

    Rust does not have classes but structs that behave similarly. They can hold properties and are quite easy to work with. However, learning how to use structs is out of the scope of this article. Struct in Rust:

    struct Person{
        name: String,
        age: u8
    }
    let person : Person = Person { name: "Jhon", age: 6};
    

    Rust also has traits that behave like interfaces ( abstracts classes ). I however find their appellation much more intuitive than interface or abstract classes. Rust does not have inheritance, but the behavior can be mimicked through the use of the default method in trait implementations. more on traits.

Learning curve

Learning Rust from a language as high as javascript can be difficult. The reason is that Rust being a system programming language, requires knowing some lower-level concepts.
That said, I must mention that the rust book is very well written and easy to understand. It goes into great detail about the reason for certain behaviors. Knowing the reason why the borrow checker goes made debugging much easier for me.

Why Rust

Rust is a good solution to build a fast and secure backend server. It is also a good choice to start writing web assembly and produce very fast front-end applications.

I chose rust as the main language to build "crawling-spider", a project on which I will be working for the next two months or so.
Inspired by scraper-copier "crawling-spider" is a visual live browser simulation of the web crawling process.

After words

I am still very new to Rust. Some of what has been said may be wrong. Going forward, I plan to build a portable server mock in Rust as my first project.



Thanks you for reading 😁

Copyright 2021, created by Daniel kodoh