hero

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.

Door Gerard Klijs
Geschreven op 2019-02-19

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.

There was some buzz about Web Assembly and it being a good fit for Rust. I wanted to try out that way of using Rust. Web Assembly is a binary format which modern browsers can read like Javascript. Web Assembly is still in development, doesn’t have garbage collection and runs single threaded. The only way to communicate with Javascript is through numbers. Because Rust automatically frees memory when the variable gets out of scope, it doesn’t need runtime garbage collection. The only type which exists both in the Javascript world and the Web Assembly world is a number. There are several crates available that make it easy to work with other data types like strings or arrays by doing the conversions. There is a crate to easily bind functions in Web Assembly to Javascript functions. I have read a book specifically for Rust and WebAssembly 🕸. Reading the book and using its instructions resulted in a 3d game of life. Moving the examples from the book to the third dimension and doing the calculation for the next state is even more complex. The book explains how to publish the project to npm. From there it can be used in other projects, with a Typescript definition file that you get for free since Rust is statically typed. Web Assembly is supported by Webpack 4, but it’s a bit of a hassle to set up right.

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.

There was a project using Web Assembly and Rust by Sendil Kumar. He explained the benefits of using Web Assembly over Javascript and why Rust was a good language fit for creating Web Assembly code. It was a good presentation and he had some nice examples. It was clear that it’s not really ready for production yet, but a lot of people are working together to get it there.

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.

Spreken op TEQnation 2019

Op woensdag 15 mei vindt TEQnation plaats in Utrecht. Dit is de grootste Multitrack Tech-conferentie van Nederland voor alle softwareontwikkelaars. Als je kijkt naar dit evenement, is het niet gek dat Open Web sponsor is en dat onze Open Web collega's Ivor Boers & Michel Drenthe hier een talk doen. De talk gaat over de avonturen van autonoom zeilen.

banner-image