Technology

Hurry-driven learning: A Rust DNS client

May 15, 2015

Read time 4 min

If your functional and/or system level coding skills are rusty, you can improve them greatly by making them Rusty!

Rust is a fairly new programming language that grew out of a hobby project of Graydon Hoare, a developer at Mozilla. Mozilla started sponsoring the language’s development in 2009. Rust is developed in the open and its source code is licensed under various open source licenses. Lots of different features and strategies have been tried out during its development, so early adopters have suffered from various breaking changes. A stable 1.0 release is planned for May 2015, with launch parties scheduled around the world for today, the 15th of May.

Rust tries to get as close to metal as C. The interesting twist is that at the same time, it also frees the programmer from manual memory management and provides high level abstractions such as static and very fine-grained typing, type inference, first-class and higher-order functions, immutable data structures and pattern matching. More or less since Java gained popularity in the late 1990s, the group of languages for getting serious stuff done has been split into the high-performance, low level of abstraction group on one hand and low-performance, high level of abstraction on the other.

I have become interested in Rust via some enthusiastic workmates who have been coding in it for a while. For example, Joonas Javanainen has been developing Mooneye GB, which is a Gameboy emulator written in Rust. When I had the chance to participate in Reaktor’s annual code camp training and use whatever language I liked, I ended up going with Rust.

Even though the coding problem itself was only revealed in the beginning of the camp, the organisers had told us in advance that we should be prepared to communicate via JSON transmitted via UDP. So, in the little free time I had at hand, I decided that I wanted to do something minimal with Rust that would do something clear and understandable with UDP.

UDP is mostly used for both DNS and multicasting. I had a quick look at whether I could have made a client program that would stream music via UDP from some free public service in the internet, but didn’t find anything that would have seemed easy enough. Hence, I decided to start coding a dummy DNS client. The spec I had in mind was that if I gave it a DNS name on the command line, it should tell me the IP address, if one was found 🙂 So just plain and simple A record query.

Installing the Rust beta or nightly build is easy enough, with both pre-built binaries and scary curl | sudo sh install scripts found on the site. I downloaded it, started writing “hello, worlds” and command line and /etc/resolv.conf parsing, after figuring out that getting the first name server line from resolv.conf was good enough for my purposes. Lots of new languages come with a nice REPL, but Rust didn’t have one. I downloaded and installed Rusti separately, but it didn’t seem to remember the functions or variables very well, so its usefulness was limited. Fortunately, the Rust compiler and the Cargo build tools are blazingly fast (at least to me who’s spent the last fifteen years more or less on top of the Java Virtual Machine!), so having a simple inotifywait loop running on a terminal next to the code editor was good enough continuous compilation and running.

The coding itself reminded me a lot about Haskell. In a constant battle against the compiler, I resorted to loads of repeated unsafe kludges. The strict static typing helped drive design with function signatures, and when I finally got something to compile, it always did something sensible. The syntax is well-thought-out and nicely lightweight, even with semicolons.

In the intense two days of coding in the code camp I learned more than enough Rust that I could easily clean up the code quite a bit, to a state where there isn’t overtly much mutation or other ugliness and most errors give meaningful error messages.

Even with the steep learning curve, Rust is easy enough to get started with for a small program. The installation package was really planned so that you land running. Just go ahead and play with it – have a go at the future of system programming!

Never miss a post