2016-07-10

Rust

From time to time, I write small programs to fulfill a need, or just because I have some ideas I want to experiment with. I used to fire my text editor and my C++ compiler to code such programs. But lately, I've been playing with the Rust programming language, and I think it became my favorite language at home.

Of course, the main list of features found on Rust's home page is a source of motivation when you are looking at programming languages that fall in the same category than C and C++: memory safety, explicit ownership... including some radical features like the move semantic, i.e. a variable is moved by default instead of being copied, or the fact variables are read-only by default. I would love having "const" by default in C++.

But lately, what made me reconsider my default programming language at home was the Rust ecosystem, something I feel is lacking with C++. With Rust, you don't only get a compiler, you also get:

  • a good and quite complete standard library. I started with C++98 and in that era everybody were rewriting their standard framework. Fortunately that's now better since C++11 and other recent iterations of C++.
  • a build system. This have been a major issue of time loss for me on both Windows (tweaking Visual Studio project files, which format tends to change from one version to another) or on Linux, where I sometimes default to GNU Make (but creating a full build system with GNU Make is painful) or QBS, provided with QtCreator. With Rust, you get a full build system (called Cargo), and that really saves time, for you own code and when you try to build someone else's code.
  • an infrastructure for third-party libraries. Integrating a library in my C++ projects has always been painful. With Rust you just go to crates.io, check the dependency you would like to add, and then add a line in your Cargo build file. That line will lead to the automatic download, build and integration of the third party library. That too is really time saving and makes code reuse easy.

I wanted to run on Linux an old program I wrote on Windows in C++ with my own framework. But instead of adapting the existing C++ code, the reasons I just listed made me decide to rewrite the program from scratch. The program is a tool to fix subtitle files. Sometimes subtitle files are not well formatted, or some small mistakes can be found that would easily be fixed automatically.

In addition, I found other several design decisions that pleased me: easy and incentive unit testing, low dependency (you can in fact write an OS with Rust), or some syntactic sugar. For example, I've always been bothered by nearly-useless parenthesis around some C++ statements:

if( value == 42 ) { plop(); } // That's C++, {} are optional for single line operations
if value == 42 { plop(); } // That's Rust, {} are mandatory, but you don't need parenthesis

I could go on about that language. I am still learning and there are still plenty of features I need to play with. Do I sound enthusiastic ? ;) I have to find some counter points:

  • the entry ticket can be high. C++ requires discipline to avoid mistakes, but it does not enforce any good practice rules. The Rust compiler will enforce them. I love that, but it can feel like you are fighting with the compiler.
  • contrary to a lot of recent languages that focus on productivity but with a compromise on performances, Rust follows the same tracks than C++ in that regard. The cost can be a higher complexity. For example, you have const char* and std::string in C++; in Rust you have str and String. Rust will hardly replace languages in domains where C and C++ are already not suitable.
  • the language is only one year old. Well, it started several years ago, but version 1.0 came last year. It means there are still libraries and tools to be built around it. In other mature languages, you get a lot of out of the box facilities. The lack of maturity and advertisement may also frighten people that are not closely following the tech industry.

One last word. You want to try Rust without installing anything? You can write Rust online! Go to the Rust Playground were you are greeted with a "hello world" program. Click Run to build and run the porgram. That's a fantastic initiative to learn and to exchange code snippets.