19 février, 2018

I recently gave an introductory talk about the Rust language, to experienced programmers who did not know the language. The presentation can be seen here and downloaded on Github. This article is a short summary of what I described.

The idea was not to enumerate features, but to highlight some of the cool things that have made this language so popular and growing quickly with a lot of enthusiasm.

I chose to focus on the 4 following elements:

Tools

Borrow checker

Community

Integrations with other languages

There are more of course, but hey, attention and time are limited resources, tech talk are no exception.

Tools

TL;DR:

3 cools things:
– cargo. It does a lot of things, and do it well and simply
– the rust compiler. It’ll teach you how to program.
– nice integration with the existing IDEs.

The cool thing with Rust is that the tools work well, and they work well together. One of the angular stone of this tools is cargo. According to the cargo book:

One of the cool things with all these commands is that everyhting is embedded in one tool, with clever default settings; also, cargo can be extended, and new commands can be added. rustfmt, for instance, can add a new command for uniformizing the code style.

I’ll talk again about cargo in the “community” section, regarding the dependency management.

Next, the compiler is your best friend. There’s this saying that, if your rust program compiles, it will work. Let’s take an example in C++:

What will it print ? But more importantly, what should it print ? Do we want to add an unsigned int with a float ? what type should be the result value, an int or a float ? Should we accept negative values ?
A c++ compiler won’t complain, and execute “incorrectly”:

$ g++ main.cpp -o cpp -Wall -ansi -pedantic
$ ./cpp
4294967293

Actually, it does exactly as we asked: the substraction lead to an underflow, and we reached the result we asked. But it’s not what we wanted.

The idea is that there is no implicit in Rust; well, there is, but as little as possible. The compiler can (and will) infer things based on what is writen so that the code is not too verbose, but most of the time you’ll have to be clear about your intent.

The goal for this is to make sure that the program will execute what is intented, and for that intentions need to properly be specified.

Another cool thing regarding the tools is that Rust works well with the existing IDEs. You can see on AreWeIDEYet which tools to install in order to customize your development experience, and have an integrated debugger, code completion, compilation errors next to the code, etc.

The borrow checker

The borrow checker is one of the major ideas that Rust brings. Let’s take an example, imagine we have the following piece of code :

In most languages, it would be fine. You searched for a string inside the text, you’ve got matches, then you modify the text. Ok, whatever. Not in rust, if you take advantage of its power. With an implementation of search that uses references, you’ll get an error:

This is awesome. This error says that we cannot modify contents, because some other data are linked to it: that’s the case because the content of matches is directly linked to the content of contents, memory-wise. They point to the same data. So you can’t modify the content like this.

This feature of the langage takes a bit of error to get accustomed to, but it eliminates whole classes of bugs.

The community

The community is really active; Rust community has a strong open-source culture, so a lot of things happen in the open. There’s been a call for writing blogpost about improvement requests for 2018 (look for #Rust2018). Before that there was a implementation frenzy. A lot of things are published everyday in Rust’s subreddit. There is also a weekly newsletter, “This Week In Rust”. It contains news about the language and community, as well as contribution requests for various kind of developpers.

Many libraries (they are called crates in Rust’s lingo) can be found and used very easily thanks to crates.io. A cool trivia is that crates.io is written in Rust. If you want to find crates for web development, take a look on AreWeWebyet.

Using an external crate is very easy: you simply have to add a dependency to your Cargo.toml metadata file. Here, I added clap, for command line argument parsing:

It may not be impressive if you come from a nodeJS, python, ruby or PHP background for instance. They have their own dependency managers that does this kind of thing more or less similarly. But think of it from a C++ perspective, where people use to work with CMake in order to deal with external libraries. It always took an afternoon to get the configuration right, and half the time things did not work because of incorrect library version that you had to fix by hand.

Integrations with other languages

Rust works well with other languages, thanks to what is called FFI, Fluent Foreign Interfaces. You can write Rust code and execute it in C, C++, python, Ruby, or the other way around. Let’s take an example where you will export a Rust function and use it in a C program. You start by saying you’ll build a dynamic library:

Of course, it can deal with more complex arguments (strings, objects), but the basic idea is there (more examples on Rust FFI Omnibus)

Another cool things is that since december, there is a webassembly build target in Rust compiler. It means it’s very easy to build a Rust program that compiles to webassembly, and that can be used with a javascript application. Rust-roguelike is an example of bidirectionnal javascript <-> Rust communication, where you can easily see how to do that.

This rust build target is also already used in a mozilla component for source maps, that you have maybe used without knowing !

Final notes

There are many topics I didn’t talk about, like:

generics

traits

functional programming

unsafe

macros

concurrency

but I’ll leave that as an exercise to the reader 😉 We have already covered a few amazing things around the language that already make it really great.