June - Rust

A new low level language

Description

June seems like a good month to look into Rust. Rust uses high level paradigms while still allowing low level memory manipulation. It almost seems the best of both worlds! Is it however? The only way to find out is by trying it.

Rust is much newer than the last two languages, so it’ll be interesting to see what impact that has on the findings.

Specification

One of my favourite programming projects was in C++ - Tellas. OpenGL was a blast to use and I mixed Scala in there as well to allow some more control. It’d be enjoyable to go back and try some fancy graphics again, but this time using Rust instead of C++. So the task here is to make a spinning cube! It should have a single light source and basic lighting shaders programmed in. If there’s extra time, putting something that can be seen only in the cube would also be cool. But really, the question is whether Rust is as easy, or even easier, to use than C++ in a practical project. We’ll soon find out.

Final Report

Another month, another report. I managed to create a spinning cube with a light source using the glium Rust library, and learned a good few things about developing in Rust. Click below for a video of the result:

Shaded Spinning Cube

When I started the project, I used the instructions on the glium page to guide my progress, as it had specifics about the implementation used that would be difficult to work out myself. I immediately found that to include the library in the project, it required a shocking two lines of code. In C++ I’d typically have to fiddle with a CMake for a week’s worth of time over the project to get OpenGL to be built in the project. In comparison, the ability for Rust to automatically include dependancies is a massive pro.

Next I began by copying the template code and editing to give me more freedom. Here I began to run into more problems. Like C++, Rust has the ability to make a variable uncopyable. However, it also has a lot of emphasis on safety in doing so. This quickly became a problem where I couldn’t even access a variable twice in a row - something that wouldn’t be a problem in standard C++. I’m unsure if I missed something or if it’s meant to be this hard to use variables. Fortunately the ability to borrow variables using the & symbol made things a bit more manageable, as this allowed them to be used to some extent, however still this is different to the original type, meaning often it cannot be converted to other types using the convenient into() function.

Aside from this however, things went extremely smoothly. Creating variables for objects and lists becames extremely concise and straightforward compared to C++, and it felt as if the compiler understood what I was writing rather than spending all my time on syntax. There were some interesting things it did as well, such as using unsafe blocks where the compiler can’t guarentee correctness of code. However this felt natural and didn’t impede programming at all.

More problematic was when I attempted to parse an error. The idea was to print an error if an error was thrown by the function and the return value of the function otherwise. However programming this quickly became a challenge as returning from the match statement gave an error. The removal of null in place of these optional types is definately a step in the right direction, but using these can be inconvenient.

One other crucial issue that plagues all new languages is library support. I was unable to find a library for the vector and matrix mathematics, meaning I had to implement them all from scratch. This was slightly inconvenient, and led to bugs in the implementation of these methods to consider in addition to the bugs in the core program, but overall wasn’t a dealbreaker, and taught me a bit in putting them in myself.

Overall, Rust was actually a really nice language. For libraries supported by it, it provides a safe programming environment which makes writing code convenient most of the time. But it is something that one must get used to - becoming truly proficient with it to be able to spot and solve the errors that pop up takes time. Nevertheless, it was an enjoyable experience and I’d be happy to program in it again.



Joseph Keane

Black-Photon