A Rusty Weekend

3 minute read Published:

My first steps with the Rust programming language

The Rust Language

Since reading about Mozilla’s new programming language Rust I was eager to give it a try. Rust is a really new language and the first stable version was released in 2015. Almost C like performance without memory corruption vulnerabilities, no data races and so forth thanks to a number of very interesting design concepts and no garbage collector or any other kind of runtime overhead made that language sound really awesome.

In Rust all safety checks are done in compile time. The compiler translates into the LLVM meta language which then takes care of the optimization and compilation into machine code for the target architecture. Rust libraries provide an FFI so they can be used from almost every other language. Due to it’s safety features and the lack of performance overhead, Rust also aims to be a system programming language and there are some really awesome projects, e.g. the Redox operating system which implements a microkernel architecture and has some really great design ideas (have a look at it!). Also Mozilla started implementing a new, parallel browser engine called Servo which is more than 3 times faster than Gecko, the current engine used in Firefox1. The MP4 parser in Firefox is already implemented in Rust and there is more to come. Here is a more complete list of Rust code that runs on production systems.

Rust also has its own package manager/build management tool called cargo that makes working with libraries really easy. Libraries in the Rust ecosystem are hosted on crates.io and you just need to reference them in the cargo configuration and they will be downloaded and included in the build.

New Projects

Last weekend I started my first project to get in contact with Rust, called skeleton, which aims to be a language independent project management tool. When starting a new project, e.g. using Gradle for build management, one initializes the structure using gradle init but in most cases that’s not the only thing to do. You might want to initialize a git repository for the project, maybe copy a license file and download a gitignore file. Skeleton aims to automate these steps using some simple configuration files which allow the user to execute commands, create folders, touch files, include other skeleton configurations or download a gitignore file from gitignore.io by a given list of languages, IDEs, … to ignore. So skeleton --lang=java init might initialize a Gradle project, copy your license file, touch your README.md, initialize a git repository and download a gitignore file for Java, Gradle and IntelliJ.

Conclusion

The design of Rust makes you think different about your code, e.g. by default every variable in Rust is immutable. If you want a mutable variable you have to explicitly mark it as such. When passing a variable as a parameter to a function you can’t simply reuse it after the function returns, as long as you don’t explicitly borrow the variable for the time of the function call. All these features can also make you think different when writing C code and probably help producing better code. I really think Rust is worth looking at especially when implementing performance critical parts of a project. Also when looking for a new language to learn it is a good idea. Even for people coming from scripting languages who aren’t familiar with the edit-compile-run cycle of compiled languages it is really easy thanks to cargo. Write your code, maybe add some dependencies and simply run cargo test for your tests or cargo run to execute the program. To get started I can recommend the official documentation The Book or have a look at some examples on Rust by Example.