Telerik blogs

Rust is in its sixth year of being the most popular programming language (according to the Stack Overflow Developer Survey). Let’s learn why!

Writing JavaScript code is amazing. The possibilities of what we can do with the language today are extensive. From simple websites to powerful applications that can run on any platform, JavaScript has evolved into an amazing language for beginners.

Rust is a language different from JavaScript but it is trending in the development community. Developers love Rust—the adoption and number of companies and projects using the language grow every day.

It is not a surprise when we see that the language is the most loved (since 2016) among the hundreds of thousands of developers who take part in the Stack Overflow Developer Survey every year.

There are plenty of reasons why Rust is becoming so popular and we’re going to understand what these reasons are.

Rust

Rust is a systems programming language created by Mozilla. The language has as its main goal to help developers to write at a systems-level without having to worry about memory and thread-safety.

Rust is a fast, reliable and powerful systems programming language that is memory-efficient. It has no runtime or garbage collection and can easily integrate with other languages (say hello, JavaScript!).

Memory Safety

Programming languages are a set of instructions for a system to perform certain tasks. We have various programming languages for many different purposes. Besides all that, we can say that at the end of the day there are two types of programming languages: low-level and high-level.

High-level languages are easier to understand. Most of the time, the syntax is very friendly and we can see what’s going on under the hood. We have many high-level programming languages nowadays such as Java, Python and JavaScript.

One of the main characteristics of a high-level language is the lack of efficiency to handle memory. Usually, a high-level language does not handle memory very well and consumes more memory than needed.

Low-level languages are hard to maintain—the syntax is not easy for humans to understand, but machines understand it pretty well. Low-level languages are very machine-friendly.

The biggest advantage of low-level languages is how they handle memory. Low-level languages have a very high memory efficiency. This means that they consume less energy compared to any high-level language.

Why are we talking about high-level vs. low-level languages? Because Rust is a systems programming language that is both high-level and low-level at the same time.

Rust is a powerful language designed to be memory safe. It does not permit null pointers, dangling pointers or data races. Data values can be initialized only through a fixed set of forms, all of which need their inputs to be already initialized.

Let’s understand more about how Rust handles memory so well and prevents segfaults (specific kinds of errors caused by accessing memory that “does not belong to you”).

Immutability

Like JavaScript, in Rust we have the let and const keywords for declaring variables. We have a few differences on how they work in Rust though.

We should add a mut keyword after the let keyword when we want to reassign. The mut keyword means that the value of this variable is mutable.

fn main() {
  let age = 24;
  let mut name = "Leonardo";
}

For the const keyword, we should always add the variable type in front of the variable name.

fn main() {
  const FINAL_AGE: i32 = 24;
}

In Rust, we can only reassign a variable of the same type. For example, if we try to reassign a string value to a variable that has a number type, it won’t work and the editor will show us a “mismatched types” error.

fn main() {
  let age = 24;
  age = "24"; // Error: mismatched types, expected integer, found `&str`rustc(E0308)
}

Ownership

A garbage collector is a very important part of many programming languages (like JavaScript). A garbage collector is a process for automatic memory management. The collector manages to collect garbage code or memory occupied by objects that are no longer in use. Garbage collection frees developers from manual memory management.

Rust does not have a garbage collector. Instead, it has a feature called Ownership, which is responsible for making the language memory safe.

Ownership is a set of rules that governs how a Rust program manages memory. All programs have to manage the way they use a computer’s memory while running.
Rust documentation

These are the three ownership rules in Rust (also from the Rust docs):

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

The relationship between scopes and variables is pretty similar in Rust when we compare it to JavaScript. There’s only one very important difference: the value remains valid until it goes out of scope.

When we create a variable inside a scope, the value is valid until it goes out of scope. If we create a variable inside a scope and try to use that value outside the scope, it won’t work.

fn main() {
  let age = 24;

  // new_age is not valid here, it’s not yet declared
  { // new_age is valid from this point forward
    let mut new_age = 24;
    new_age = 25;
    // we can do anything that we want with new_age inside this scope because it's valid
  } // this scope is now over, and new_age is no longer valid
}

Ownership is a new concept for many developers, especially JavaScript developers. It takes some time to get used to. We should keep in mind the three rules of the ownership system every time we are coding in Rust.

Structs

In JavaScript, the OOP (object-oriented programming) paradigm is very widely used. Many projects use this paradigm and the chances are high that you have worked or will work in the future with OOP in JavaScript.

Rust is not an OOP language, but it does emulate some features from the paradigm and struct is one of these features.

A struct is a data type in Rust that allows us to combine data items of different types, including another structure. A structure defines data as a key-value pair.

fn main() {
  struct Person {
    name: String,
    age: i32
  }

  impl Person {
    fn introduce(self: &Person) {
        println!("Hello, my name is {} and I am {} years old", self.name, self.age);
    }
  }
}

Every field in a struct must be associated with a data type. The impl keyword is used to define a method within the context of a structure. The first parameter of a method will be always self, which represents the calling instance of the structure. Methods operate on the data members of a structure. We can use a struct to separate both data and methods and make our code easier.

Conclusion

We can see that Rust is a very powerful language that has many features to help developers create memory-safe code.

As JavaScript developers, we can see that Rust shares many similarities with JavaScript and yet is a different language. The usage of Rust among JavaScript developers is growing every day and you would benefit a lot by giving Rust a try.

While it does take some getting used to understand how a few things works in Rust, the reward is well worth it.


Leonardo Maldonado
About the Author

Leonardo Maldonado

Leonardo is a full-stack developer, working with everything React-related, and loves to write about React and GraphQL to help developers. He also created the 33 JavaScript Concepts.

Related Posts

Comments

Comments are disabled in preview mode.