in

WHY RUST PROGRAMMING LANGUAGE IS MOST lOVED AMONG DEVELOPERS?

During the past five years, RUST Programming language has become the most loved programming language on the StackOverflow. Rust has some drastic features which have made him a most loved programming language among the community of 65,000 developers on Stackoverflow. Here is the survey report which shows that 86% of developers who are using rust are keen to use rust in their future projects. This ratio is summarized in the table below

PROGRAMMING LANGUAGEPERCENTAGE OF DEVELOPERS USING AGAIN
RUST86%
TYPESCRIPT67.1%
PYTHON66.7%
KOTLIN62.9%
GO62.3%

 

The Real question is why RUST programming language is ruling over the hearts of developers since its foundation? The simple answer to this question is that it fulfills the pain points which are present in other programming languages. In every language, there is a trade-off between safety and controls. The beauty of RUST is that It gives us both safety and control simultaneously.

Here I have summarized some drastic features of this programming language which makes it superior to other Languages

  • There is no trade-off between safety and control in RUST. It provides both at the same time which makes it a charming to developers all across the world
  • Its memory map is different from other programming languages. Unlike the Garbage collector, It uses a new method of Stacks and Heap which makes it both safe and control at the same time.
  • It is an expression-based language and not statement based. This means it has IF expression, not If statements.
  • There are no dangling references in Rust due to its strict ownership rules
  • It does not contain NULL in it which makes it a lot superior to others
  • It’s a static type language and has the same speed as C/ C++ and Assembly.
  • It has a very strong compiler unlike dynamically typed languages (python etc) which catch the error at compile time.
  • It has a very sophisticated error handling techniques which make it unique and superior from other programming languages
  • It follows strong rules for memory allocation which keeps it safe from stack overflow and stack corruption
  • It has a syntax like most advanced programming languages in the world like Haskell, Asp.net
  • It has advanced data structures for easy handling of data like Structures, Enums, and Patterns

Due to its remarkable features, Rust has found its uses in embedded systems, server-side programming, and web applications. Above are the features which make RUST really superior to other languages. We will discuss each of the above features in the article below.

To know about role of internet of things in 4th industrial revolution, Click here

MEMORY MAP OF RUST:

The first thing which makes rust amazing over other programming language is its memory map. There are two types of languages Higher level languages and lower-level languages. Lower-level language (Like C/ C++, Assembly) use stacks to store data. In these languages, Variables formed on stack after the function is called. When the function returns, the variable stores on stack Cleans away. In these, there are chances of stack overflow and stack corruption (When you access the wrong variable in the stack and change its value result in program Crashing (If your luck is good) and unusual behavior of program). This is much against the program’s safety. While Lower level Language give us much control over memory as we have a variable of different kinds such as unsigned and signed integers and floats of (8 bit, 16 bit, 32 bit and 64 bit) Which Can directly access to the memory at ground level which gives it control to memory

WHAT IS INTERNET OF THINGS,CLICK HERE TO LEARN

While higher language (Such as Python, PHP) use garbage collector to dump variables after using. The use of a garbage collector slower the speed of Language but increase efficiency. Here we cannot access the wrong variable on stack and change its value (error formed). While there is no control over memory. We do not have a variable that can access memory at ground level.

Here we understand the Approach of RUST. It uses a completely different Memory Map. Its Memory Consists of two parts

  1. Stacks which contains data having fixed size at compile time
  2. Heap which contain data whose size is not fixed at compile time

Rust uses a certain set of rules to Code the memory. These rules are called ownership rules. Rust uses the concept of ownership in it which makes it safe. According to ownership, Every variable has its owner. If the variable is accessed by another variable on Stack (fixed size), it will make a copy of it and you can operate on it which keeps the original variable safe.

On the other hand, If the variable store on Stack (not fixed size), We cannot access it with other variables (Its ownership will be lost). The variable cannot be copied in heap. The above situation is illustrated in the figure below

Figure 1 illustration shows error occurred if we make a copy of heap against ownership

So we make references to operate on that variable stored in heap. If we want to change the value in Heap, we should make a mutable reference (Which we can’t make more than one time). This situation is shown in the figure below. These features make it a safe language for developers. Developers do not have to worry about memory in RUST. They just have to follow ownership rules and the rest is on RUST to manage the memory. To know more about memory layout and Ownership in rust, click here

Figure 2 This figure shows if we make two mutable references, an error will occur

TO KNOW MORE ABOUT NUMERICAL PYTHON, CLICK HERE

ERROR HANDLING IN RUST:

Another Feature which makes rust loftier to other programming languages is its error handling technique. The error handling in other languages is to simply throw an exception when an error occurs. But Rust handles it in a different approach. Rust divide errors into two kinds

  • Recoverable Errors
  • Non-Recoverable Errors

Recoverable errors are those which can be easily handled. These errors include syntax error or file missing. In this case, rust simply sends a message that the file is missing and we can simply add file or change syntax to recover it.

Unrecoverable errors where the developer doesn’t know what to do. In this case, rust becomes Panic. For example, if you want to access variables in the list above range, the compiler does not know this variable and it will become panic. So it will raise a panic error in this situation. The situation is illustrated below

Figure 3 The figure shows how panic error occurs in Rust and how it can be handled

To know more about error handling in rust, Click here

Static-typed language:

In this era of development and technology, there is always a war between statically typed language and dynamically typed language. Each type has its own pros and cons. Dynamically typed language has a very easy syntax and there is no worry about declaring variables neither in sense of type and nor in size. However, statically typed language has a very good compiler and speed. There the rust finds its importance. It is a statically typed language that has a strong compiler which detects an error at compile time. Some static type language detects an error at run time but rust detects it at compile time.

SPEED OF RUST:

Rust is a statically typed language and it has the same speed as C/ C++ or Assembly programming languages. Due to its enormous speed and control, rust has found its importance in embedded systems, server-side programming, and web applications. Rust is used in server-side programming, in which code written in rust is compiled in web assembly and it is used in servers. As Javascript/typescript is used for server-side programming which are dynamically typed languages, rust a static type language has 30% speed more than javascript in a server, 60% in mobile search engine and 90% more in firefox browser (As RUST is developed by Mozilla, same who made firefox browser and they have great compatibility with each other). Now rust with web assembly has got an extension in almost every search engine and is greatly used nowadays. Furthermore, due to its speed, It is greatly used in RTOS (Real-time operating system) which requires instant action.

Absence of null references in Rust:

Sir Charles Antony Richard Hoare, the founder of null references says

“I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object-oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years”

A null is an object that is assigned where the null value is a member of every type, defined to have a neutral behavior that has no value. Nowadays we need to have null references but we want to be safe on the other side. So rust does not provide a null reference by default. It uses a special ENUM (A custom data type for the implementation of Null). So we can be safe and can have NULL at the same time according to condition. To know more about option enum and null statement in RUST, CLICK HERE

Figure 4 An option enum which is used for the implementation of NULL in RUST programming Language

RUST an Expression based language

Rust has syntax like most of the modern programming languages of the day. Rust is not a statement based language. It is an expression-based language. In the statement-based language, we make statements. If the statement is true, run that block else other (remember an if-else statement in C/C++). But rust is somewhat different. It is expression-based and expression can return a value which means you can initiate a variable using if statement in RUST.

Figure 5 This figure shows that expression in If return a value and we can initialize variable with it

Data types and data structures in Rust:

Rust has numerous data types which are integers (signed or unsigned) of different sizes (8, 16, 32, 64 bits), floating points, strings, vectors, and hashmaps which makes it much compatible and closer to memory (Due to which it is much loved in an embedded system where we have to write directly to memory and as discussed earlier, due to its speed it is used in RTOS (A branch of embedded systems)) and it has various data structure such as structs, enums, and patterns to manage large data. These features of RUST make it much loved to programmers. Here is the detail of data types and data structures in rust.

No dangling references in RUST

To access an object which has gone out of scope or been destroyed leads us to dangling reference. This can be illustrated by the example below

In the above figure, when getX() returns, then x will go out of scope and its life will end. So in this situation, r will be a dangling reference point. Rust has alluringly controlled this matter of dangling pointer using ownership rules. The ownership rules provide us with a strong model that ensures memory accessed properly, preventing unusual and unintended behavior. Rust forbids the use of uninitialized variable and dangling pointer and hence prohibit the use of unintended data.

Let r;

{ let x=5;

Let r=&x;

}

Println!(“r: {}”, r);

When we run the program given in the above block, the Rust compiler will raise a compile-time error. The ownership rules of rust do not let an uninitialized variable to make a reference and if trying to do so, rust strong compiler will immediately raise an error of breaking ownership rules and the program will be stopped.

 

 

 

 

What do you think?

Written by Ghost

Leave a Reply

Your email address will not be published. Required fields are marked *

An Introduction to NUMPY: Numerical Python for Computation

iot market size

Role of Internet of things in 4th Industrial Revolution