August 22, 2023

The Rust programming language sent ripples through the programming community when it was first released in 2015, promising the blazing speed of lower-level programming languages without the accompanying sharp edges.

Four and a half years on, many programmers still view Rust with a mix of intrigue and trepidation due to its appealing premise and notoriously difficult learning curve.

We sat down with one of Cosive’s Senior Developers, Sid Odgers, to talk about why he believes more programmers should take the plunge and learn Rust.

How did you first discover Rust?

1.0 had just come out, and I saw a link to it. That was 2015. Quite a few people in the programming community were talking about Rust and I had no idea why there was so much buzz, and why everyone was so excited about it.

What was your first experience with trying to learn the language?

It was infuriating. The fun thing about Rust is that a lot of the memory safety stuff that you do by hand in lower level languages, and that that the tooling and runtime takes care of for you in higher level languages like Java, is handled by the language's type system and compiler, at compile time.

You get the safety that you get from high level languages, but it means keeping the compiler happy and doing things in a safe way to begin with, which also means there’s a whole other set of rules you’ve got to follow to even get the thing to work.

And so overcoming that learning curve is tough. There were a few times that I almost gave up in disgust. But the benefits, once you get your head around them, are compelling. You get all of the safety of a much higher level language with the performance of something that is effectively, basically, just C.

Did you ever have doubts about it being worth the steep learning curve?

No. I felt it was a really good idea, and it was intriguing. The learning curve was frustrating, but it helped that there was very much a light at the end of the tunnel, and that the light at the end of the tunnel was very shiny. Rust’s balance of memory safety and performance is very much a holy grail in programming.

What do you think Rust does better than alternatives people might use to solve the same kinds of problems?

I think what it does better depends on what alternatives you’re talking about. It also depends on the domain you’re operating in. In what we’d call systems programming, where traditionally it’s all been C, C++, at the really low level even Assembly language, or whether you’re talking about a higher level type of environment like Java or Python.

If you’re looking at Rust as a replacement for a lower-level language, the benefits are the memory safety stuff and the fact that you can’t have a data race at runtime, and things like that. But speed isn’t as compelling, because Rust is just as fast as C one way or another. But unlike C, you don’t have to do all that mental book-keeping to stay memory safe.

On the other hand, if you’re comparing Rust to Java or Python, or something a lot higher level, then you’ve got the same safety benefits and a lot of the same higher level constructs, like generic data structures—maps, lists, things like that—but you’ve got a compelling speed advantage. You also don’t have a garbage collected runtime to carry around with you, which has a whole bunch of other benefits. The code you write can run on its own without runtime support which means you can turn it into a library, or use it wherever, without having to worry about possibly having two runtimes fighting about who gets to free the memory.

Ultimately, the benefits depend on where you’re trying to use the language, which I think is one of the really big advantages of Rust. It will fit almost anywhere. From that giant vertical segment of “What programming language will I use” all the way to “Well, I’ll replace C with Rust in this OS kernel” all the way up to “Maybe I’ll do this in Rust instead of Python” for something much higher level. Whichever way you slice it, you get some benefit from it.

What are some use cases where you’d consider using Rust?

The classic example is if you’re writing software in the systems domain, something like a network server or something with really high performance requirements, but you want to stay safe against memory bugs and prevent data races between threads. Rust’s type system guarantees two threads can’t unsafely interfere with each other’s data. The whole concept of a data race that you might have in other languages, where you might forget to use a locking primitive like a mutex, simply can’t happen in Rust. Because the type system will say that if you want to share values between threads you need something that’s safe to do that with, e.g. one of the locking types, or something that’s atomically guaranteed to be safe to share, like an Arc (atomically reference counted smart pointer).

When deciding whether to use Rust, look for situations that will play to its strengths. Anywhere memory safety or correctness of concurrent data access is going to be a problem, and you still need that really low resource usage, really tight code generation, extreme control over memory that you can get out of a low level language. If you want to do all that without blowing your foot off, and having yet another memory access bug that leads to a remote code execution exploit, then it’s worth considering Rust.

But it’s certainly not a free lunch. There’s an awful lot of cognitive load that comes with working in Rust instead of C because all of a sudden you have to keep the compiler satisfied. It goes “If you’re doing it this way, look, it might be safe, but there’s a universe of possibilities that exist where it’s not, so I’m just going to blanket reject it.” Because the thing can’t read your mind. It’s not an AI or anything like that. It doesn’t have reasoning capabilities. It just knows that programs that look like this are, in a certain percentage of scenarios, wildly unsafe, and so it rejects all of them.

Someone who is used to a more traditional language is going to have a decent learning curve getting comfortable with the new idioms, the new patterns, and with learning what the right way to do certain things is. The challenge is to both keep the compiler happy, and keep the code reasonably performant. But these are all things you only have to learn once.

What are some things that helped when you were first learning the language?

The Rust community is amazing. There’s also an official book, which is up to v2 now. The book basically covers everything that you need to know about learning the language, all the common pitfalls that you might face. It’s a fantastic resource. There’s a whole heap of things along those lines. The official forums, the Subreddit is great. There’s just a really good community full of people who know what they’re talking about and are genuinely decent human beings.

Do you have any favourite Rust libraries?

The ecosystem evolves, big time. I couldn’t really name any one particular library. The ecosystem is broad, and I don’t want to play favourites. Today’s cool thing isn’t tomorrow’s cool thing. It does touch on a slight achilles heel of the ecosystem though - everything is a bit immature. The pace of development is at a very high velocity, everything’s changing. It’s only a four and a half year old ecosystem. Maybe ask that one again in five years and I’ll have a good answer for you.

What would you say to someone who was on the fence about learning Rust?

No matter what programming language you expect to be using in five years, learning Rust now will make you better at it.

Is there anything else you’d like to say about Rust?

Again, unlike the traditional lower level languages like C, Rust has all the higher level tooling that you’d expect from a modern language. It’s got a package manager that supports semantic versioning, dependency management, all of that. It’s got a first class reformatting tool. There’s only one correct way to format a given block of Rust code, so it’s really easy to write idiomatically formatted code. You just run it through the formatter. All of the little bits that aren’t necessarily part of the core experience, that sort of bolt on to the side, are all absolutely first class as well.