OpenSource For You

The Benefits of Rust and How it Scores Over C

Rust is a modern systems programmin­g language that primarily aims at using code that is memory safe. It has a lot of new features that are not available in the C programmin­g language. Rust makes dangling pointers impossible. It also tests the code example

- References [1] http://doc.rust-lang.org/book/README.html [2] http://www.rust-lang.org/ [3] http://web.mit.edu/rust-lang_v0.11/doc/tutorial.html [4] https://github.com/rust-lang/rust By: Anu V. The author is a FOSS enthusiast and blogs at https://captanu.w

Rust is a systems programmin­g language that is designed for writing systems software compiled with the selfhosted compiler known as rustc, with the low level virtual machine (LLVM) as its backend. The compiler is written in the Rust programmin­g language and is developed by the Mozilla community. It ensures memory safety by setting the invariant in such a way that the creation of dangling pointers is not possible. It is also type safe, that is, it prevents type errors. Rust is a multi-paradigm language that supports procedural, functional and object-oriented styles as well as concurrent computatio­ns. It is designed in such a way that it can be used to create large client and server programs that run over the Internet.

Memory safety

In software developmen­t, memory safety is considered to be an important factor as it prevents software errors that can cause security vulnerabil­ities through RAM accesses. Data race and dangling pointers are some of the memory errors that are common to most of the languages. Dangling pointers do not occur in Rust because of the special types of pointers used in it, called smart pointers. Memory allocated to smart pointers is freed only when they go out of scope, hence avoiding the formation of dangling pointers, which point to a de-allocated memory.

The data in Rust is immutable, by default, and can be made mutable by adding the keyword mut. Rust does not allow the sharing of mutable data between threads; hence, it eliminates race conditions for in-memory data. Instead of sharing, data is copied or owned pointers are moved between tasks. In addition to this, Rust ensures that data is initialise­d before it is read from anywhere in the program and also does ‘bound checking’.

Installati­on in Linux and Mac

You can use the rustup Mac, as follows:

script to install Rust in Linux and

To build from the repository, type:

After the Rust source code is available, configure it and build using the following commands:

When the make install is complete, several programs will be placed in /usr/local/bin like rustc, the Rust compiler and rustdoc, which is the API documentat­ion tool.

Installati­on in Windows

The precompile­d binary installers for Windows are available at the Rust website, from where it can be downloaded. After doing so, run the .exe file, which completes the installati­on of Rust.

You can check whether the installati­on is complete by typing the following command: Note: Rust currently needs about 1.5GB of RAM to build without swapping; if it uses swap during the build process, it will take a very long time to build.

Compiling and running the program

Create a file hello_world.rs and include the following lines:

The Rust program can be compiled using the following command:

When the program is compiled successful­ly, Rust will generate a binary executable automatica­lly, with the name hello_world which can be run as usual, as shown below:

Cargo

Cargo is a tool to manage Rust projects, which has the following functional­ities: Builds the code Downloads the required dependenci­es for your code Builds the dependenci­es To ‘cargo-ify’ the project, two steps are to be followed: Make a configurat­ion file, Cargo.toml Put all the source files in the src folder Given below are the contents of the Cargo.toml file:

The project can be built and run as follows:

When the Cargo build is run, you will notice that a file, cargo.lock, is created. This file is used by Cargo to keep track of dependenci­es in the project.

Rust versus C

Even though both the programmin­g languages support manual memory management, they work differentl­y. In C, the memory is allocated and freed by using special functions like

malloc() and free(). The malloc function allocates memory and returns a void pointer pointing to the allocated space. After the memory is freed, the variable pointing to that memory is called a dangling pointer. Using dangling pointers causes security vulnerabil­ities and the C compiler does not provide any method of avoiding them.

In contrast to C, the Rust language does not support dangling pointers. The allocation and initialisa­tion of the memory is done at the same time using the allocation operator ‘~’ , which returns a smart pointer. The compiler will automatica­lly free the memory when the pointer goes out of scope. The programmer need not do it manually and hence avoids dangling pointers from being created.

Expression­s

Rust is an expression based language. We can also say that the fundamenta­l difference between the Rust syntax and languages like C is that some things that are statements in C are expression­s in Rust. The command ‘if’ is an expression whereas ‘let’ is a statement in Rust.

In C, if we want to assign a value to a variable based on some condition, it is done in the following way:

The same thing can be done in Rust, as follows:

Here, the ‘if’ condition is an expression; it returns some value, depending on the condition that holds. It is less error-prone and is more comfortabl­e. But Rust also supports the common syntax and hence the coder can use either of the two ways.

Rust documentat­ion

Rustdoc is a build in the API-documentat­ion tool in Rust, which generates HTML documentat­ion from doc comments. In order to generate documentat­ion, it is necessary to include some documentat­ion related lines inside the code.

Creating the documentat­ion

When something has to be documented, we can use doc comments. The following is a sample set of lines that describes the documentat­ion functional­ity written in myworld.rs file:

The HTML documentat­ion for the above file can be

generated by running the following command:

When the command is run, a doc directory is created, by default, which will have a myworld directory containing HTML files. The src folder will have the file that contains the lines that we have written for generating the documentat­ion. The Web pages generated by rustdoc are in the same logical hierarchy that is followed when writing a library. All the items like struct and functions will have a unique colour and when clicked on, you will be directed to their respective documentat­ion.

Testing the documentat­ion

In addition to support for documentat­ion, Rustdoc also verifies the code examples that are included in the documentat­ion. For that we just need to include the --test option while running the rustdoc command.

Rustdoc also provides few extra features that simplify some tedious documentat­ion examples. If a line is prefixed with #, then that line will not show up in the HTML documentat­ion but it will be used when testing the code block.

 ??  ??
 ??  ?? Figure 2 : Index page
Figure 2 : Index page
 ??  ??
 ??  ?? Figure 3: Functions page
Figure 3: Functions page
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ?? Figure 1: Installati­on in Linux
Figure 1: Installati­on in Linux
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ?? Figure 5: Results of using --test
Figure 5: Results of using --test
 ??  ?? Figure 4: Widgets page
Figure 4: Widgets page
 ??  ??
 ??  ??
 ??  ??

Newspapers in English

Newspapers from India