I’m an intermediate C# developer that’s been working with .net for about as long as I’ve been a developer, around 3 years. I am still learning a lot every day, just as most of us, but I feel that my understanding of programming has improved since I started playing around with another language, Rust. 

But what is Rust? – It’s a systems programming language that focuses on performance, stability and productivity. It has no garbage collector, and the performance is as close to C as you could get. Looking at the productivity standpoint, that’s a harder question to answer, C# is quite easy and productive to use. From my point of view as a C# developer I like the fact that I get speed, but my main win in this all is the stability of the application.  

Rust has two main areas that I really like: first one is the borrowing system, a ruleset that forces you to think about memory management. The second part it the exhaustiveness, how the compiler forces you to handle all your cases.                                                                                   

These two has helped me a lot with my C# development and made me realise how much is handled by the garbage collector in C#. You rarely have to think about memory management in C#, not unless you count the IDisposable interface, which in itself takes time to understand why and how to use.  

“Think of it as if you had a book in your hand, you can either lend it to the next person or give it away”

In Rust you instead have to think about memory as you go. Each time you are leaving a scope e.g. method, Rust will try to clean up what this scope is done using. You have two ways of passing memory: by reference as a pointer with a simple ‘&’ before the variable name or by actually passing the value. Think of it as if you had a book in your hand, you can either lend it to the next person or give it away. The book is a variable and you are giving it away, after that you expect to use it again, this is where Rust will tell you that ‘Hey, you’ve moved the value!’.  

This mindset of actually taking care of your memory and not just blindly passing data everywhere is hard to grasp to start with, but as you go there’s a lot to learn from it. You quickly learn why data races or concurrency problems fade away and your code becomes really stable. 

Returning to the exhaustiveness of Rust. There are no exceptions or nulls., instead it has Panic (and die) and two simple enums. The enum in Rust is special in that It can contain anything, even methods and structs. Combine this with the fact that all iterations need to be exhaustive, Rust forces you to state action on all cases unless you specify a standard way, much like Elm does. The two most commonly used enums are:
Option<T>{ Some(T), None } & Result<T,E>{ Ok(T), Err(E) } 

Want to get number 5 in a vector of 3 items? You get a Option of None. Want to open a file but the file is not there? You get a Result of Err.  

This all seems too much work, but thinking of the impact it makes on the application, it becomes really stable. Since all of this is means more work, it also means that Rust has a lot of syntactical sugar around this subject to help ease development a lot. 

I’ve mentioned correctness in memory management and Exhaustiveness, but why are we not using it more? The simple question is that it’s still new, but it’s also rising in usage a lot within big and small companies. If you ever have an application that needs speed and stability, have a look at Rust! 

Author: Alexander Herlin, developer at One Agency