Some appealing Rust features seen through JVM/C eyes
The Rust language keeps gaining popularity. This article covers several Rust traits and features that catch the eye right away.
Introduction
The Rust language keeps gaining popularity. You can find it in all sorts of tooling, in the Linux kernel, in Android, and in other systems.
Rust is a statically typed language that is fairly young (compared with, say, C or Java). This creates both opportunities and difficulties. Building a fresh ecosystem from the ground up means there is no backward compatibility to preserve. It can draw on experience gathered on other platforms and make bolder changes.
In this post, we’ll look at several Rust traits and features that catch the eye right away.
No Null’s!
In many languages, nulls are a nightmare. Java projects often resort to defensive programming, checking every incoming value to make sure it is definitely not null. In Scala, best practice says not to use nulls, but they still exist at the language level because of the JVM and interoperability with Java libraries. Tony Hoarse called his introduction of null in ALGOL his “billion-dollar mistake”. Pretty much everyone who has worked with Java has run into a NullPointerException at least once. Rust, by contrast, simply doesn’t have them. Every situation that other languages deal with via null is covered here by an Option enum.
Immutability by default
Variables in Java are mutable. Some projects sprinkle the final keyword everywhere in an attempt to make them immutable, yet even that offers no firm guarantees (because an object assigned to a final variable may still have mutable fields). In Scala, mutability is considered bad practice. Case classes and vals are immutable, though mutability is available when you need it. Rust goes down the same path: every variable is immutable unless stated otherwise. Why? A quote gives the answer:
An immutable object is thread-safe in every case. - Brian Goetz
When you do need mutability, you can get it with extra modifiers.
Tooling - code formatting
Rust’s tooling comes with built-in documentation (Rust book) and a code formatter. As far as I know, Java has no standard for code formatting. Scala has the widely used scalafmt tool, yet not every project adopts it. In Rust, formatting is simply part of the toolchain, which can make adoption far easier. Cargo additionally ships with Clippy, a powerful Rust linter.
Concurrency features
The JVM memory model is a magical realm. Discovering what it is and how it operates can be mind-blowing. Writing concurrent code with the volatile keyword is hard, and the actual order in which operations happen is far from obvious. C doesn’t make things easier: race conditions and similar problems are a frequent headache.
Rust tackles this in an entirely different way. It brings in a handful of concepts (many of them known earlier but absent from the most popular languages) – lifetimes, borrowing, and immutable references. Combined, they let the compiler verify that your code is safe. Rust stops you from doing things that might cause problems at runtime.
Dependency model
On the JVM, every project dependency lands on the project classpath, and a project relies on just one particular version of a given library. If a conflict occurs, the newer version wins. That can, however, result in runtime errors reporting that some class or method could not be found.
In Rust, Cargo is aware of Semantic Versioning. The crates your project relies on can themselves rely on different versions of one library, and the compiler takes care of it for you. Moreover, thanks to the dependency rename feature, you can even use two versions of the same library explicitly in your own source code.
Performance features
Java offers several kinds of loops, such as the classic for and foreach. I recall old arguments that foreach was slower! Quite similar articles popped up around Java 8, when lambdas arrived. Rust is in a somewhat different position. Its closures and iterators are implemented in a way that leaves runtime performance untouched. Rust strives for zero-cost abstractions, so that more sophisticated features cost nothing at runtime and are dealt with at compile time instead.
Conclusions
Rust is fascinating. It addresses several pain points commonly encountered in the JVM world, and its design decisions bring obvious benefits compared with, for example, C or C++. It isn’t the simplest language, but it is certainly a powerful one. Today it has carved out a niche as a C substitute in a range of tooling and system-level implementations. Rust is flourishing, and there are plenty of web frameworks for it too. Still, building a basic web application in Rust can be harder and take longer than in, say, Java. If you don’t need the performance it offers, Java/Scala may be perfectly sufficient.