I recently got to experience programming in rust and it has been one of the best programming experiences of my life

by Arsen in September 17th, 2021

First of all, how did I get the opportunity to use Rust?

I got to use Rust as a tool to develop a Web assembly Project without having to write the complex web assembly code directly. The preferred method here is using the wasm-pack rust library which helps compile rust code into corresponding web assembly code.

Why Web assembly?

Well, Web assembly is like the assembly for the web in the sense that despite being executed on a browser, it runs programs with a near-native performance which couldn’t have been done before with just javascript.

How was my first time experience with rust?

To be honest with you, the first look at a rust code was pretty daunting.

What’s the deal with having two different data types for string: “&str” and “String”. Also, you are telling me there is no corresponding type for null?

However, after dabbling into the world of rust a bit more, I came to understand, just how well thought of these peculiarities were in making a programmer's life easier.

Enum Option replaces null

First, let me deal with the less obvious one: Why is there no null type in rust?

Null was first introduced more than half a century ago, so it is safe to say that it’s quite an established concept in the field of programming. It used to be considered a boom for programming. However, now as programming is evolving, programmers have come to realize that it’s more of a bane than a boom.

Let’s imagine the following code which tries to get model of the discrete GPU on a computer like such:

String version = computer.getDiscreteGPU().getModel();

However, many computers don’t actually ship with a discrete GPU. So, what would be the result of this call? A common bad practice is to return a null reference to indicate the absence of a discrete GPU. Unfortunately, the getModel() method will try to return the Model of a null reference which will result in a NullPointerException at runtime and stop the program from running further. Imagine the program running on a client’s machine, every time he/she tries to run the program it just never runs and he/she never knows what the problem is.

Here in the words of the creator of null Tony Hoare himself:

“In 1965, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.”

Although Rust doesn’t have a null type, it provides an enum Option that can encode the concept of null as an optional value that can be present or absent.

Enum Option {

Now with the Enum Option, the code immediately shows if the computer has a discrete GPU or not (since the discrete GPU is optional). This way the program clearly knows if a given value is allowed to be missing and takes proper measures even when it’s not present.

In a nutshell, the enum option includes methods to explicitly deal with the cases where a value is present as well as where it is absent. Here, the advantage compared to null reference is that the option enum forces you to think about the case where the value is not present. As such, you can prevent unintended null pointer exceptions.

Rust's compiler is awesome. It tells exactly what's the problem.

Another thing I like about rust is Rust’s compiler is very good at telling us what’s the problem. For example:

error[E0308]: mismatched types
--> src/main.rs:3:11


3 |     greet(hello);

|           ^^^^^^^

|           |

|           expected struct `std::string::String`, found `&str`

|           help: try using a conversion method: `hello.to_string()`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

Here, it's clear that we’re dealing with two different data types: std::string::String, aka String, and &str. While greet() expects a String datatype, apparently what we’re passing to it is something of type &str. The compiler even provides help on how to fix it. We just need to convert hello variable to &str using .to_string() method and indeed using greet(hello.to_string()) works!

The difference between &str and String is that String is a growable, heap-allocated data structure whereas str is an immutable fixed-length string somewhere in memory. Since the size is unknown for str we most commonly only use it as a reference: &str. &str can be used to view data that is stored anywhere: in static storage, inside heap-allocated string, or even on the stack which is very convenient. There are times when we need to own string data to constantly update it so we use String in that case but there are other times when we only need to view or use string owned by someone else so we use &str in that case. By having two different data types, it ensures there are no misbehaving strings in a program.

So far, I am absolutely enjoying coding in rust and I wish to continue coding in rust as much as possible. If you are up for a new, enlightening and enriching coding experience like I have I would absolutely recommend you to give rust a try.

UNiD Edge SDK repo is available on our GitHub. Please come and visit if you want a deep dive into it.

Your cart