Skip to Main ContentSkip to Footer

What Is Rust?

October 26, 2019

What is Rust?

The Rust programming language was created by Mozilla and its first stable version was released around 2015. It looks like this:

fn main() {    println!("Hello World from Rust!");}

Rust enables everyone to get performance and reliability out of any code you write, it’s like doing pair programming with the compiler guiding you when you write your code.

It means that it’s blazingly fast and memory-efficient: with no runtime or garbage collector and its rich type system guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time, even before you ship your product!

Why should I care about Rust?

You can use Rust to supercharge your JavaScript, one module at a time. Thanks to WebAssembly you can make Rust and Javascript work alongside, to help supercharge processing-heavy or low-level tasks — tasks that benefit from Rust’s focus on performance!

You can read more about Rust with WebAssembly here:

Rust Types

JavaScript is a dynamically typed, and its type system is not strict.

In Rust, if you try to perform a simple addition of two numbers that are not of the same type you get a compilation error:

fn main() {    let first32: i32 = 42;    let second64: f64  1.0;    println!("{}", first32 + second64);     // ERROR: first32 and second64 are not of the same type.}

The main Types of Rust are the following:

  • i8 for integer of 8 byte, u8 for unsigned integer, they grow up of various sizes like 16, 32, 64 and 128.
  • f32 and f64 are floating-point types
  • bool for boolean
  • chars for characters

In this case I have also added an underscore cause Rust compiler forced me to use “_” to define variables that won’t be used.

Rust Immutability

In Rust, we will use let to declare variables, and they will be immutable by default, won’t be able to change them. If we want to use mutable data we need to add the mut keyword to the declaration like this:

Rust Ownership

One of the hardest concept to learn in Rust is Ownership cause it is really different from other languages but this is the key that makes Rust faster and safer to use than other programming languages!

When you assign some data to a variable it‘s said that the variable owns it and every piece of data can only have one owner.

Rust Classes

There are no classes in Rust but you don’t have to worry, you can simulate classes behavior in Rust with struct and impl.

You define structures of data with struct combining various data types together and then you use impl to specify specific functions to be executed only for those struct types.

In this case, we defined an animal base “class” and we derived a class “dragon” calling the base class functions and defining its custom behaviours.

Rust + Javascript = Love

You can start using Rust now! No need to install anything to start testing it.

Just check here and start to modify lib.rs. In this case, I modified the start example to concatenate the string “from Rust” and the Rust compiler helped me on every change, suggesting to add to_owned to clone the string.

This public function “greet” is called from Javascript, you won’t notice any difference to a normal greet function written in Javascript but you will get the safety and speed of Rust all running on your client-side or also on the server side.

You can fork or edit my Rust Playground here.

If you want a more basic rust playground without Javascript you can use this other website playground.

Where can I learn more about Rust?

You can start to understand more of Rust language, types here.

You can read more about Rust + WebAssembly + Javascript here.

Conclusion

Rust is a really cool language to learn and you could build a lot of things with it, it has a steep learning curve but you will get much more productive after some practice being able to work together with the Rust compiler to become a better developer!

Learning Rust will help you became a better Developer by doing virtual pair programming with the Rust compiler!

References and Resources:


Subscribe to my email list!

If you enjoy my work, you should definitely join my newsletter"Giorat Mails". It’s one email a week with everything interesting I’ve read or found, plus new articles and fresh tutorials.

Sign up for our newsletter