Introduction to Rust Programming

Introduction to Rust Programming

Since the official birthdate of the Internet, January 1, 1983, the Internet has come a long way. With advances in Software development facilitated by tremendous growth of the Internet, Software Development is growing faster than ever. From developing applications for room sized computers to developing applications for computers of size just as a coin, many requirements have been changed, but the need for performance has been the same forever. This explains why Developing in C and C++ is still relevant. While they do have their issues such as memory management and security, it is hard to match the performance level of C/C++ . This is where Rust Programming Language comes to help. Rust combines the ease of development, Performance and Secure Memory management all under one hood.

According to a 2019 survey by stackoverflow, Rust has been the most loved programming language for the fourth year in the row. Rust Development was started by a language engineer named Graydon Hoare in 2006. After several years into development, it caught the attention of Mozilla and it was launched in 2012.

One of the main benefits of programming in Rust is that it facilitates zero-cost abstraction, meaning, it does not matter what type of code you use, loops, closure, etc. It will all be compiled to the same assembly without any overhead or affecting the performance of the code in any way.

Most low level systems programming requires the choice of programming language to have control over low-level memory. While memory control of C is powerful, it can be a nightmare if implemented incorrectly.

Unlike most memory safe programming languages, Rust does not need to implement a garbage collector. A garbage collector is a feature implemented by programming languages such as Haskell, Python and Ruby. It automatically identifies the memory no longer in use and frees up the memory. But as it works by constantly analyzing the memory and cleaning the memory at run time, it costs a lot of overhead and those applications take performance hits. So, to circumvent this issue, Rust implemented Ownership. Whenever a function is executed, it is added to the call stack including variables stored and their values. This creates a scope system. That means, values and variables defined on the stack can only be accessed by the owner method of the stack. As it is not possible to store larger data on stack, it is stored over a heap memory which is of larger size and the pointer reference is then added to the stack. So, when a function is done executing, the respective data of the function stored on the stack is deleted to clean up the memory. As this does not require a garbage collector, rust can save a ton of execution time. This faster run time is one of the reasons why more and more programmers choose to write in Rust for Embedded programming.

In addition to its performance benefits, Rust ecosystem is constantly evolving with its rising popularity. Rust is facilitated by a command-line tool named cargo which helps managing dependencies and running tests.

Rust's community site, crates.io provides the necessary libraries for the needed development

Developing a dice game in Rust

Step 1: Install Rust and Cargo

It is easier than ever to install rust in your macOs / Unix based systems.

You can directly execute the following command in your terminal to automatically download the installation script with curl and install

curl --proto '=https; --tlsv1.2 -sSf http://sh.rustup.rs | sh

If you are using windows, you can download the offline installer and install

You can also use the in browserrust compiler from the rust to compile and run your program.

image.png For the demonstration, I am going to use the rust compiler and cargo installed on my linux system

First, create a folder where you are going to perform your exercise.

After that, use the cargo new hello_world command to create a new cargo environment.

image.png Now you can go inside the src directory and open the main.rs for editing which will contain the code

image.png Currently it contains a simple hello world code, you can compile the code with command cargo build and execute it with command cargo run.

image.png Consider following code snippet

use rand::Rng;

fn main(){

println!("Future prediction!");

let roll = rand::thread_rng().gen_range(1..6);

println!("Your dice roll was {}", roll);

match roll {

1 =\> println!("Your health is in risk"),

2 =\> println!("You will soon find love"),

3|4|5 =\> println!("You are going to lose money soon"),

6 =\> println!("You are going to win a lottery"),

_ =\> println!("error"),

}

}

At line 1, we are using a crate named rand and importing Rng from it.

As we are using an external crate, we need to add it to our cargo.toml file.

Cargo.toml file should look like following

image.png Here, we have added the rand and the appropriate version that we require under the dependencies section.

Now we can run cargo build and cargo will automatically download and setup rand crate on the system

On line 3, we have the starting of the main function. In rust, main function is written as fn main()

We can use println!() to print a string, it prints the string provided and adds the new line character at the end.

On the next line, we have defined a variable named roll with keyword let. roll is initialized with a random value from 1 to 6 using thread_rng().gen_range(1..6) 1..6 can be replaced with the desired random number range and then we print the random number using a println! Statement.

{} inside the println! Function will be replaced by the value of the variable specified as the argument to the function.

Just like there are switch cases in other languages, you can use match statements to do the same task in rust.

We are using the match to compare the value of the dice and print the related future.

We can also match more than one values separated by |

image.png Just like Hello world program, you can use cargo build and cargo run to compile and run the program

image.png Conclusion

Learning curve of Rust is hard, but once mastered, it can be a very helpful tool in your arsenal to develop fast and secure applications