News - 23.10.22

Why we choose to be Rusty

If you were to take a look under the hood of the stacuity technology stack, you’d find quite a lot of Rust. 

Not a corroding pile of old metal, but software written in an innovative programming language which is quietly revolutionising how system software can be built.
stacuity | R

The early days of programming involved writing code which would then be directly processed by the hardware – instructions to the electronics shuffling the zeros and ones around. Being a programmer involved thinking like a computer – it was specialist work, laborious and error-prone. Over the decades that follow, programming languages have become richer – increasing programmer productivity by simplifying the job. It has gradually become possible to think more about the intention of the software – and less about what was going on under the hood.

This started with languages such as C – and then over the decades, gradually developed into languages which mainstream programmers use today – such as Python, JavaScript, C# and PHP. In addition to increased productivity through abstraction and efficient reuse, this evolution helped reliability, security and safety – the ‘runtime environment’ of such languages keeps an eye on what the programmer is doing – reducing the likelihood of ‘shooting themselves in the foot’, tidying up the garbage they leave behind, and to some extent mitigating security risks through erroneous or malicious activities.

However, increased abstraction tends to be accompanied by reduced efficiency – in terms of the balance of ‘useful work’ compared with overhead which the computer has to do. This directly translates to power consumption, memory capacity and so on: wasted effort on the part of the computer. A paper presented by the ACM ranked 27 programming language in terms of their energy efficiency. Taking C as a reference value of 1.00, JavaScript score 4.45, PHP 29.3 and Python 75.88. In other words, Python consumed over seventy times as much energy as C to do the same job. Even C# and Go – generally regarding to be pretty efficient – scored 3.14 and 3.23 respectively.

Rust, a language conceived by Mozilla Research, takes a different approach to the ‘efficiency and safety’ vs ‘productivity and abstraction’ conundrum. It attempts – and does a pretty good job – of giving us the proverbial cake whilst we eat it. It achieves this in two ways:

Firstly, the syntax of the language (the format and structure of the instructions that the programmer enters) is designed to make it nigh-on impossible to erroneously write code which leaves the software open to security vulnerabilities, leaving memory in a mess, or creating race conditions (such as ‘catch-22’ scenarios when the program logic grinds to a halt waiting for itself).

Secondly, the Rust compiler – which is responsible for converting the instructions provided by the programmer into a format that the underlying hardware can understand – has incredible power which borders on witchcraft. The vast majority of mistakes or oversights that a programmer might make are spotted – and rejected – by the complier, so never make it into the resulting system.

There is an old adage that the earlier a bug is found, the cheaper it is to fix. The best time to spot a bug is whilst coding (or before). If it is missed, and is only found during testing, the cost of fixing it could increase tenfold. If it evades testing and is only found during production use of the software, the cost could increase tenfold again. On this basis, the fact that Rust helps to identify bugs so early in the lifecycle is a real advantage.

Rust’s genuine novelty isn’t going unnoticed – and it is getting attention from the highest echelons of the software industry. Mark Russinovich – Microsoft Azure CTO – recently tweeted that where C and C++ had been used previously, Rust should be used instead. Only in the last few months, Rust has started to be used in the Linux kernel – until now, a hallowed place where only C and C++ should enter.

When it comes to that energy efficiency metric, with C setting the benchmark of 1.00, Rust comes in at a very close second – with a score of 1.03.

There are, of course, downsides. Firstly, the language omits some features which many programmers are used to. Inheritance? Not really. Exceptions? Definitely not. However, these are not oversights – the nature of the language requires a different approach, and in working with Rust it soon becomes clear that there are better ways – and the resulting software is often cleaner and clearer as a result.

Secondly, learning Rust is a commitment – it has a steep learning curve and early forays can be frustrating and unproductive. It is worth persevering though – once mastered, Rust programming is very rewarding – like solving a complex equation or playing a beautiful medley depending on your idea of reward.

So, why does Rust play such an important part in the stacuity technology stack? In the core of our mobile network, we need to be able to shuffle packets around at very high speeds – tens of millions per second. We need to do so safely and securely, and to avoid latent bugs which are lurking to jump out and bite us. When we are not coding, we like to sleep at night – and Rust really is the only feasible option which ticks all the requisite boxes.

(We use other languages too – for example C# and Go in our control plane, TypeScript for the web. We use Kafka (among others) to glue the components together – and Rust plays nicely with the others.)
New languages come along reasonably frequently – but many provide only incremental improvement (or changes) to what came before. We believe that Rust is different – and are excited to watch its wider adoption.

The Rust logo is property of the Rust Foundation and appears under Creative Commons Attribution 4.0 International license.

Related content