Gerard zijn ervaring met rust
Rust is een algemene, gecompileerde en low-level programmeertaal ontwikkeld door Mozilla en bedacht door Graydon Hoare. Rust is deels geïnspireerd op de programmeertaal C, maar kent syntactische en semantische verschillen ten opzichte van C. Rust is een functionele programmeertaal die focust op veiligheid. In het Engels vertelt onze developer Gerard Klijs zijn ervaring met deze taal.
Rust is an open source programming language from Mozilla. On wikipedia it’s classified as a multi-paradigm systems programming language. It can be seen as a memory safe version of C/C++. But what is it like to program in Rust and what is it actually used for?
Most of my development time is spent writing Java in the context of web development. I have also done some pet projects in Clojure, a modern Lisp that runs on the JVM. A couple of times I came across an article called Rust for Clojurists. It made it seem like Rust is rather easy to learn by explaining some of its concepts in a clear way, even though Rust is often mentioned as one of the hardest languages to learn. It has probably also become a lot easier since 2015, when the language was stabilized with the 1.0 release. The article also rightly mentioned three weak properties of Clojure, which are some of the strong points of Rust: performance, safety and embeddability.
About a year ago I decided to just give it a try. First I was working with The Rust Programming Language. There are some concepts that are new to Rust that are not easy to get right, but the book clearly explains them. The book also provides exercises to become familiar with them. I used IntelliJ IDEA as my IDE. Several advanced features like auto-import don’t work as smooth as with Java. Debugging is also not possible, this wasn’t really a problem, because once it compiled, it was working.
After working through the Rust Programming Language book I wanted to create a small application. As a pet project I already had an event sourcing demo, consisting of a couple of small applications. One of which is using both Kafka and PostgreSQL and I rewrote it in Rust. It turned out there were already great libraries, called crates, available to do most of the heavy lifting. But the pet project was set up with the Confluent Schema Registry for which there was no easy solution. By combining several other crates I did get it to work. At that point I had an application in Rust which was able to handle messages created on the JVM, send a message back and have that message turned into a typed Java object again.
It was quite a hassle to find the correct crates to work together, and also to get the specifics right to read / write the bytes in the correct way. Writing the code for dealing with bytes was definitely easier then doing byte manipulation with Java.
Since I was also interested in the crate system, I decided to make a crate of this part of the application. Towards releasing it as a crate I added more tests. In Rust you can write tests as documentation, right above the function you document. These tests are run each time you run all the test. So when you write some examples as test you are sure they always stay in sync with the code. My crate is available on crates.io. Releasing a crate is really easy as all you need is a Github account to create an api token.
A few days ago I went to my first meetup about Rust, where I shared my experience of open sourcing part of the application I built. There were several small talks and demos.
There was a project where Rust was used for embedded devices. Roy Buitenhuis shared some tips and tricks to leave out some checks in the compiled code of Rust in order to make it fit better on embedded devices. He also ran several benchmarks comparing Rust with C. While the compiled code with Rust used more storage space, its memory usage and speed were comparable to C. Because of the checks in Rust the code was safer.
Jeroen Bakker talked about using Rust with Blender. He was telling about simulating groups of people and the scaling problems they had using Python. They had rewritten it in Rust which made it much more performant, able to render large groups of people. This was mainly because of the fearless concurrency feature of Rust.
Rust was featured in some projects from Technolution. Erwin Gouda talked about a few of those projects. One of them was part of a system to make it easy and fast to control cameras used in maritime traffic. The video feeds were sent real time to the controlling posts making it possible to quickly switch cameras. There was a setup with multiple cameras to try it yourself. The setup used webkit to combine the control with the video from the camera, making it easy to remotely control the camera.
The other interesting project they were working on was to detect hack attempts on military vehicles by sniffing all the network traffic.
Jan Blom shared his experience in replacing Java code with Rust. They had an immune repertoire application which did a lot of calls to C. With Rust it’s easy to create bindings to C which are fast to execute. As it turned out with Rust the program was four times as fast.
While it’s not easy to program with Rust, it’s not as hard as it used to be. Even without any C or C++ experience I never really got stuck. At some points you can get a bit frustrated because you think the program almost compiles and then it gives an error. Most of the time it’s easy to search for the error and fix the code. When used with some crates for the heavy lifting it’s a pretty similar experience to programming in Java or Clojure.
Rust is already used in parts of the Firefox browser and other big companies like Dropbox. At the meetup there were several other examples of using Rust for products already in production or getting there. Recently AWS has added support for running Rust as a Lambda as well which could be an interesting option to start using Rust when you are already running on AWS. A lot of work has been done to be able to write frontend code completely in Rust. There are several frameworks but it’s all still at an early stage and I would not recommend using Rust for the frontend for now.