Rust – the programming language

Submitted by peter on Sat, 05/19/2018 - 18:44

Rust is a programming language designed to compete with the C and C++ programming languages with an emphasis on safe code. Rust is free, open source, and backed by big organisations. What is there to not like?

The official Web site for rust is www.rust-lang.org. The latest version, when I looked, was 1.26.0 from May 10, 2018. The install uses rustup as the installation and maintenance tool. Rustup creates .cargo/bin in your home directory. The new directory contains the rustc compiler, rustup, and cargo.

Linux Mint 18.3 contains rustc 1.24.1 in the official repositories. The description says it is a 68 MB download and will use 201MB on disk. Linux Mint 18.3 also has cargo 0.25.0 with a similar download, which suggests cargo installs everything from rustc.

There are manual installs. Cargo installs rust-init.sh. You can download rust-init for your platform, a 12 MB download for Linux.

You can also download the Rust package direct plus a signature file. rust-1.26.0-x86_64-unknown-linux-gnu.tar.gz is a 226 MB download. The download expands to 14,474 files chewing up 820 MB. The same manual download lets you access the beta 1.27 version and the 1.28 nightly build.

Documentation

The documentation for Rust is in the form of a book with a first and a second edition with the second edition almost finished to the point where it will be available in print.

The first and second edition are both in the 226 MB download.

C, C++

Rust is designed to compete with the C and C++ programming languages with an emphasis on safe code. Safe code is the default. If you want to squeeze some extra C level performance, you can optionally switch on C style unsafe code.

In practice Rust performs equal to C for projects with similar development times and resources. While C can be faster, debugging C can chew up the time you want to allocate towards performance optimisation. On many projects, the development time will be long enough for the processors to increase in speed by more than the difference between Rust and C.

Go

The Go language is designed for a high level of concurrent processing and leaves out many useful programming features to achieve the maximum concurrent performance. Go is great for some times of applications. You would choose Go if you wanted to replace Apache, Nginx, or Node.js. Rust is for everything else.

GUI

Does your application require a Graphical User Interface? Rust can connect to user interfaces like GTK through connectors you add into Rust. As an example, the GTK connector is at gtk-rs.org. The GUI code might work like Rust or might not. Conrod is a GUI written in Rust and should work the way Rust works.

Your GUI application may not benefit from Rust unless you are attempting to something like updating data in the background while showing different data in the foreground. Rust will handle the concurrency.

My interest in Rust started with the creation of a C + GTK application. The application is slow to start due to the huge amount of data the application has to read at the start. The application could use two components running in parallel in different processing cores, one to read the data and one to display the data.

The background part could continue reading data independently of the display part. With concurrency solved by Rust, the application could process multiple data sources in parallel.

The display could react to user activity by changing the display across available data. Parts of the display could change to reflect available data. You can do this without safe concurrency but there can be problems. The problems can be worse when you want to update some of the data while the rest of the data is retrieved. Rust should remove some of the concurrency problems.

PHP

Rust could be an alternative to PHP and Java for Web sites. Using Java is almost dead. PHP 7 makes the performance difference of something like Rust just too small to make the change worthwhile.

Web sites use the Apache Web server for flexibility or the Nginx Web server for speed. PHP has a built in Web server for small tests. There is a Web server written in Rust and offered as a step up from the PHP built in option but not yet an alternative to Nginx.

If you started with the Rust based Web service and added small Web services, the overhead of programming in Rust would be small because there are Rust libraries for JSON and many other Web related things.

You would have a problem with building a content management system due to the huge amount of code you have to write, compared to using a PHP framework. Rust may eventually get something close to the Phalcon framework then grow from there. Phalcon was a big step forward from PHP up to PHP 5 but is of less interest with the improved performance of PHP 7.

Syntax

Rust code is similar to C/C++ but different. The extra safety built into Rust does require code changes compared to C. You specify ownership details to keep data in existence while needed and to make the data disappear when no longer needed. You differentiate between data that can be changed and data that cannot be changed.

The changes are all good. While the syntax might not be the best choice, some of the functionality changes would be useful in other languages.

The differences are explained in the Rust documentation. The Rust documentation makes Rust easier to understand than some of the weird stuff in C. While there is more to learn in Rust, you may end up with a working application faster than with C, except for the GUI part.

Conclusion

Rust is worth looking at when you need some concurrency or a higher level of code safety than C or C++. Rust is worth a look when your application has to run faster than a Python application.