The benefits of the Rust programming language in research
September 18, 2021
But despite its comparatively insignificant usage, Rust was the most loved programming language of them all in 2021. In fact, 2021 marked the sixth year of Rust holding the title of the most loved language among surveyed developers!
In the context of Stack Overflow Developer Surveys, “loved” means that developers who’ve worked with a programming language in the past year will keep using it next year. With that in mind, 86.98% of respondents said they were planning to continue using Rust, and only 13.02% said they would ditch Rust for something else.
Aside from being a favorite for the majority of developers, Rust’s adoption has been steadily rising over the years. Only 3.2% of the participants of the 2019 Stack Overflow Developer Survey were using Rust. In 2020, the adoption of Rust was already at 5.1%. And in 2021, 7.03% of developers had Rust in their toolsets!
Remarkably, one of the sectors where Rust’s popularity continues to increase is science and research, according to the Nature journal. Even though Rust is no easy programming language to learn, it possesses certain characteristics that have made it the number one choice for many researchers.
Rust’s humble beginnings were spawned with a 2006 side project by Graydon Hoare working at Mozilla. Elements of Mozilla’s Firefox browser are written in Rust, while developers at Microsoft are recoding parts of the Windows operating system with Rust. What makes Rust stand out from competing languages, and how can it be useful in scientific work? Let’s take a look!
What’s so special about Rust?
Rust is gaining popularity in research and scientific circles – why is this happening? Well, the Rust programming language has a number of distinct features that set it apart from its research-oriented competitors. More specifically, Rust is designed with a heavy emphasis on code security; performance; and community, support, and usability. Let’s take a closer look at these characteristics!
When it comes to research, code security can be invaluable, especially with data that is confidential or provides a competitive edge in a given industry. Rust employs a whole arsenal of measures that dramatically increase the security of its code compared to other languages.
The central component of Rust is a safety feature called ownership. In simple terms, ownership ensures that values are only usable within the scope they have been created in.
Ownership’s behavior is based on three rules (Klabnik and Nichols, 2019, p. 98):
- Each value has a variable called its owner.
- Values can only have one owner at a time.
- Values are dropped when their owner goes out of scope.
Compliance with these rules is checked at compile time without any performance impact.
Prevent references to memory that has been freed
To help developers ensure that no references point to memory that has been freed, Rust offers another feature called lifetimes. The primary function of lifetimes is to invalidate references to values that drop after going out of scope. Consequently, lifetimes allow developers to avoid memory-related bugs or vulnerabilities.
Concurrent attempts to access a memory location
Rust is additionally engineered to prevent data races – situations where two or more threads concurrently attempt to access a memory location. At the same time, Rust makes concurrent programming efficient and simple.
Prevent bugs and facilitate debugging
These and many other safety features combined are intended to prevent sensitive data from being accessed by unauthorized code or malicious actors. Safety aside, Rust’s features can also prevent bugs (facilitating debugging) and memory leaks.
Unsafe Rust vs. Safe Rust
For those who are feeling more confident in their secure coding skills, there is Unsafe Rust – a version of Rust that lacks the language’s core safety features. However, for most situations, Safe Rust – which is the “true” form of Rust – is likely the much better choice.
Speed is another considerable advantage of Rust over its competitors used in research (Python and R, most notably). In fact, Rust is on par with C in terms of performance and even beats it in some situations. And C, if you didn’t know, is one of the fastest programming languages in existence!
According to the Nature Journal, Heng Li – a bioinformatician at the Dana-Farber Cancer Institute in Boston, Massachusetts – tested several languages on a biology-related computational task. The data used in the test included 5.7 million sequence records. Rust turned out to be the fastest tool for the job, outperforming C. C language still is a worthy contender performance-wise, but Rust is safer by design, which makes it the optimal tool for security-oriented use cases.
Granted, Rust isn’t as beginner-friendly as the likes of Python, and it’s not as well-known as C, but it can outperform them substantially. With massive datasets containing millions of data points, the speed and parallelization of Rust can be a valuable asset for data-heavy research projects.
Community, support, and usability
Rust’s active community and the well-developed documentation considerably ease the entry of researchers into the circles of Rustaceans (people who use Rust), compensating for the language’s pronounced learning curve.
The Rust team maintains detailed guides on various aspects of the programming language:
- The Book (The Rust Programming Language)
- Package manager Cargo
- Rust error index
- Basics of Rust
- Rust Cookbook
- Learn Rust With Entirely Too Many Linked Lists.
The error index is perhaps the most useful part of Rust’s documentation. Containing detailed explanations of errors and examples of bad code, this resource makes error messages seem a lot less menacing and obscure. Users may also run the example code in their web browsers – a feature that is present in some other sections of the Rust documentation as well.
Rust’s programming rules and syntax may make new users frequently scratch their heads. However, the wealth of educational resources and best practices available online should be able to make the entry smoother for them. Of note is the range of Open Source projects created with Rust.
What is the Rust programming language used for in research?
In its technology feature “Why scientists are turning to Rust”, the Nature Journal brings up a number of scientific areas the programming language has found use in.
- Case 1: Stephan Hügel from Trinity College Dublin, Ireland, had originally used Python to design an algorithm for converting geospatial coordinates from one reference system to another. At some point, Hügel decided to move his project to Rust – after two or three months of effort, he was finally able to do so, achieving a fourfold increase in performance.
- Case 2: Luiz Irber – a bioinformatician at the University of California, Davis – reimplemented a genomic search and taxonomic profiling tool called Sourmash in Rust. Irber’s switch to Rust was motivated by his desire to facilitate software maintenance, gain access to modern language features, and make the code work in a web browser.
- Case 3: Rob Patro’s team of computational biologists at the University of Maryland, College Park, used Rust to create a gene-expression analysis tool called Terminus. Patro’s team was led by graduate student Hirak Sarkar who had completed an internship at biotechnology company 10x Genomics that uses Rust to develop open-source tools.
Clearly, Rust’s adoption is accelerating rapidly, although incorporating it in a research environment can be challenging due to the language’s not so simple syntax and coding rules.
In the end, the Rust programming language is a worthy alternative to other, more “mainstream” tools when safety and performance are of high priority.
The learning curve of Rust is quite steep, so researchers may need to dedicate a few months to get the hang of all its intricacies. But once beginners break the entry barrier, using Rust will likely be a rewarding and, more importantly, productive experience. Try your hand at an introductory course and see what you think? Numerous reference texts are available to support your entry to this sphere of influence.
With that said, Rust isn’t the only option for research and statistics – many other programming languages exist, and they may be way better than Rust in certain situations. So before committing to a programming language, it’s important that researchers understand the context-tailored features and limitations.
Klabnik, S., & Nichols, C. (2019). The Rust Programming Language (Covers Rust 2018). No Starch Press.
Matthews, B. (March 10, 2019). Programming in Rust: the good, the bad, the ugly. Hackernoon.