Introduction to Rust

Rust is a multi-paradigm system programming language, which means that it can be applied in different models of computation and at the same time it can be used as a system programming language for different platforms.

Rust mainly focuses on safety, safe concurrency, and syntactically partially similar to C++ language, which is a multi-purpose object-oriented language.

Rust is also designed to provide better memory safety compared to other programming languages of object-oriented type. When we talk about memory safety, it means, that it does not permit null pointers, dangling pointers or data races in safe codes.

Through a fixed set of forms(which requires their input to be initialized), the data values are initialized.

In cases of data structures such as a linked list or binary tree, the function can be replicated in some other language of pointers. To check whether the pointer is valid or Null, the Rust core Library provides an option type which can be used to test whether the pointer has some values or is a Null pointer.

Things to know about Rust.

Paradigms Multi-paradigm: concurrent, functional, generic, imperative, structured.
Designed by Graydon Hoare
Official Developer The Rust Project
First Appeared July 7, 2010.
Stable Release Can be checked in the official site of Rust.
Typing discipline Inferred, linear, nominal, static, strong.
Implementation Language Rust
Platform ARM. IA-32,x86-64, MIPS,PowerPC, SPARC, RISC-V
OS Linux, MAC-OS, Windows, FreeBSD, OpenBSD, Redox, Android, iOS.
License MIT or Apache 2.0
FileName Extensions .rs, .rlib
Official Website of Rust www.rust-lang.org

Why Programming

Of course, almost everyone visiting this article is aware of the importance and beauty of programming. But standing in an unbiased manner for newcomers, we mention below some strict points about why programming.

  • Programming is the only way to interact with the computer and make it use the way you want.
  • Programming lets you make software for different uses.
  • Programming lets you think creatively.
  • Programming is what programming does.

Now, Why Rust?

  • Rust inherits its properties from different programming languages such as- C, C++, Cyclone, ML, OCaml, SASL and Haskell. Therefore, it has properties of Object programming Languages, System Programming Languages and also statically typed functional programming languages.
  • Rust's code can be made Reusable, by making use of modules. The modules in Rust contains functions, structures, or even other small modules inside it. The modules element can be made global and can be made public so that other modules are able to access it.
  • The Rust compiler performs all the necessary functions to enforce memory safety and all other checkings that make the language safe. For example, in Rust, you will not have problems of dangling pointers(pointers which still points in deallocated memory) or using an object after it has been freed.
  • Rust is better in error handling. Rust, like every other programming language, has two kinds of errors- recoverable and non-recoverable. But the recoverable errors are handled very safely with T and E concept, we shall later.
  • Rust supports concurrency and thread. Rust provides the way to spawn the thread through the help of the OS it uses, also reducing the negative effects of threading.
  • Rust supports complex data type collections. Its standard library consists of several popular as well as useful data structures, which can be used in different developments. The data structures are: Sequences, maps, sets, and a miscellaneous type.

Installing And Getting Started with Rust

There are not many steps involved while getting started with Rust Programming. It is an open source software, can be download from the internet and can be used. We are discussing the installation in Windows OS.

Below are the steps to install Rust

Ste
p 1: Downloading The Software

To download the software, you have to visit the official website of Rust https://www.rust-lang.org/

and navigate to the install option.

rust-install-button

After clicking the install button, you should see the RUSTUP.INIT.exe file on the next page.

rust-exe-file

Click on the file highlighted in the image and get the software downloaded into the system. It will take a maximum of 2 minutes.

Step 2: Installing the software

To install the software, just open the software and click it, and you must see the below window popping up.

running-rust-installation

After hitting run, the below window with the command prompt open up.

selecting-default-installation

After selecting option 1 and hitting enter, the following window appears- Rust is installed now!

rust-installed

Now we can close the command prompt and go to the .cargo folder to see our installed files and packages.

installed-files

Step 3: Creating a new package.

To create a new package, we open our command prompt and type:

cargo new file_name
create-new-program-package

Step 4: Running the first program

As we created the package called my_first_rust_program, inside it, a folder called src has been created and a default program file called main.rs is created, which contains our first hello-world! program of Rust.

To do this, change the directory to src folder in the command prompt.

changing-path

Now, to run the main.rs inside the src folder, we first need to compile the file and then run the file. To compile, we have to use the keyword rustc and then the program file name.

rustc main.rs
compile-and-run

After the compilation, an executable file called main.exe is created inside the same folder. So, to execute that, we type: main and hit enter.

main

And get our first program executed! Cheers!

Some probable problems while compiling rust

There are different kinds of problems that may arise while compiling Rust. Such as: Missing Dependencies Problems, MSVC linker problems or C++ Redistributable modules missing problems.

If any of these error persists, then while compiling, the errors would appear and compilation will be aborted.

Untitled

All these problems can be sorted out by installing the Visual C++ Redistributable packages into our Windows PC.

Download The Visual C++ Redistributable.

Navigate to the https://www.microsoft.com/en-in/download/details.aspx?id=48145 and then download the Visual Redistributable 2015, 2016 or 2017.

Installing the Visual C++ Redistributable:

installing-vs

After the successful installation of the packages, the error which was showing while compiling should eliminate and our Rust program should compile.

Advantages of Rust

  • Rust can work on different platforms and also can be used as a functional, statically typed and system programing language.
  • Rust is more secure than the language from which it has been inherited. Such as C or C++. Rust focuses more on consistency and better memory safety.
  • Rust requires only the operating systems and no additional software is required for compiling Rust programs.
  • Rust codes are reusable. This is done through modules which are accessible by other modules.
  • Rust supports a complex collection of data types.
  • Rust is very good at error handling.
  • Rust is a well-designed programming language.
  • Rust inherits its properties from different programming languages such as C++, C, Haskell, cyclone, ML and OCaml, and thus Rust has all properties of functional programming, statically typed language as well as system programming languages.

Disadvantages of Rust

  • Learning Rust may not be that simpler, and also requires a good knowledge about C++ or any object-oriented language.
  • Since Rust is a system programming languages also, therefore performing simple kinds of tasks also may take lots of understanding and uses of different syntaxes.
  • In Rust, the codes cannot be developed as fast as can be developed in other scripting languages like Python or Perl.
  • There is no garbage collection in Rust.
  • The Compiler of Rust is very slow compared to the other typical programming language compiler.
  • The binary files which are created in Rust are much larger but the codes are not efficient to that level.
  • In case of scripts which are more than a thousand lines, the compile time may be much higher, maybe around a minute or two.
  • Rust provides less aliasing information compared to C or C++.

Rust Vs C++

Since, Rust has a lot of properties inherited from C++. Therefore, the comparison between Rust and C++ has to be done, so that, it can be well understood that, what are the improvements that are made in the Rust. And also what are properties which lack in the Rust.

  • In Rust, the zero-cost abstraction mechanism allow us to reduce and even avoid the run-time cost. The case is similar in the case of C++, here also, the zero-cost abstraction mechanism allows us to reduce and even avoid the run-time cost.
  • In Rust, the moved object cannot be used, as a static analyzer does not permit it. On the other hand, in the C++ the move constructors are to leave the source object in a valid state.
  • In Rust, the compiler can rely on static analyzer which does not permit the use of moved object, to optimize the code. On the other hand, in the C++, at the compile time, the use-after-move errors can be detected by the external static code analyzer.
  • In Rust, The Raw pointers can be used inside unsafe blocks. These blocks can be automatically detected by tools. On the other hand, in C++, Raw pointers can be spotted by manual code review.
  • In the case of buffer-overflow, In Rust, range checks are forced by all slice types. On the other hand, in C++, range checks are forced by the explicitly coded wrapper classes.
  • In Rust, at the compile time, the detection and prohibition of data races are done by the built-in borrow checker and Rust reference model. On the other hand, in C++, data races or concurrency errors are taken care of by good programming practices and discipline, knowledge and careful review.
  • In the case of Object initialization, In Rust, before used or checked by the compiler, all the variables must be explicitly initialized. On the other hand, in C++, to initialize all object fields, its necessary to use user-defined constructors.

Summary

In this article, we have learned that Rust is a statically typed language, which also has equal uses as a system programming language as well as a functional programming language.

Rust inherits its properties from different languages which are itself stronger. Such as the, C, C++, Haskell, Cyclone, OCaml, and ML.

When Rust was developed, the main intention was to create a language which can run on a different platform and also can be executed in various computational methods.

It cannot be said whether Rust is superior or inferior to C or C++ or Haskell or Cyclone, but it's sure that the scope of Rust is much larger than those languages.

“Everybody should learn to program a computer, because it teaches you how to think”

- Steve Jobs.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions