The Benefits of Rust and How it Scores Over C
Rust is a modern systems programming 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 programming language. Rust makes dangling pointers impossible. It also tests the code example
Rust is a systems programming 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 programming 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 computations. It is designed in such a way that it can be used to create large client and server programs that run over the Internet.
In software development, memory safety is considered to be an important factor as it prevents software errors that can cause security vulnerabilities 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 initialised before it is read from anywhere in the program and also does ‘bound checking’.
Installation 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 documentation tool.
Installation in Windows
The precompiled 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 installation of Rust.
You can check whether the installation 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 successfully, Rust will generate a binary executable automatically, with the name hello_world which can be run as usual, as shown below:
Cargo is a tool to manage Rust projects, which has the following functionalities: Builds the code Downloads the required dependencies for your code Builds the dependencies To ‘cargo-ify’ the project, two steps are to be followed: Make a configuration 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 dependencies in the project.
Rust versus C
Even though both the programming languages support manual memory management, they work differently. 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 vulnerabilities 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 initialisation of the memory is done at the same time using the allocation operator ‘~’ , which returns a smart pointer. The compiler will automatically 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.
Rust is an expression based language. We can also say that the fundamental difference between the Rust syntax and languages like C is that some things that are statements in C are expressions 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 comfortable. But Rust also supports the common syntax and hence the coder can use either of the two ways.
Rustdoc is a build in the API-documentation tool in Rust, which generates HTML documentation from doc comments. In order to generate documentation, it is necessary to include some documentation related lines inside the code.
Creating the documentation
When something has to be documented, we can use doc comments. The following is a sample set of lines that describes the documentation functionality written in myworld.rs file:
The HTML documentation 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 documentation. 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 documentation.
Testing the documentation
In addition to support for documentation, Rustdoc also verifies the code examples that are included in the documentation. 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 documentation examples. If a line is prefixed with #, then that line will not show up in the HTML documentation but it will be used when testing the code block.
Figure 2 : Index page
Figure 3: Functions page
Figure 1: Installation in Linux
Figure 5: Results of using --test
Figure 4: Widgets page