Modal Title
DevOps / Software Development

Rust Makes Us Better Programmers

The learning curve for Rust may be steep, but it’s well worth it.
Dec 12th, 2022 11:09am by
Featued image for: Rust Makes Us Better Programmers

Voted “the most loved” programming language in the Stack Overflow survey for seven years in a row, Rust has already established itself in the software industry. Thanks to Rust-friendly technologies like WebAssembly and WASI, it’s also finding its way into the cloud. It’s no wonder, since Rust makes for a better development experience, enhances safety and increases program speed. But Rust also makes you a better developer. Here’s how.

To understand why Rust is so popular, we need to consider why we invented compilers and programming languages in the first place. In the 1950s, we wrote programs with assembler, which was extremely difficult to write and read, while making mistakes was extremely easy. Then in the ‘70s, we got C, which is still widely used. It is much better than an assembler — much easier to write and read, and you get some help from the compiler to find mistakes in your code.

So the reason we created programming languages and compilers is fourfold:

  • Easy language: We needed something concise, easy to write and read, and more like English or mathematics.
  • Make a program portable: We needed to run our program on different platforms.
  • Optimize: We wanted faster programs that take advantage of our hardware.
  • Finding Errors: We wanted to find the mistakes we made while writing our code.

The latter two are where Rust really shines.

It Helps Us Fallible Humans

When we are new programmers, we make many mistakes (remember when a for-loop was mystical?). Over time, we gain experience and get used to working with complicated software architectures. This is what enables us to make more complex mistakes.

These mistakes cost companies millions in both time and money invested in finding and fixing complicated, subtle memory errors occurring in complex environments. With Rust, many of these are entirely avoided.

Let’s be honest: We need more help from our tools to write code more safely and build reliable software from the ground up. We need a trainer; like Yoda, Rust trains developers.

Because Rust has a rich and strong type system and a deeper, stricter compiler, programming is challenging for a beginner. But if the code compiles, it probably works! This teaches us delayed gratification: In the beginning, it’s difficult and slow; over time, it’s an enjoyable process, and we have fewer bugs to fix.

I used to get many runtime errors when I wrote C or C++. I may be a bad programmer, but with Rust, once I understood its principles, I got fewer errors, which made me enjoy programming more and even helped me feel more competent.

It’s a Low- and High-Level Language

I love that Rust gives us a sweet balance between capability and abstraction. That is, it’s both a low-level and a high-level language. On a low level, Rust offers:

  • Raw memory pointers (though for advanced programmers, this breaks safety guarantees.)
  • Can run embedded
  • Compiles to machine code. No intermediaries overhead.
  • Customizable memory drop

On a high level, Rust provides:

  • Automatic memory drop
  • Functional patterns (iterators, combinators, pattern matching)
  • Traits (similar to interfaces)
  • Easy multithreading libraries
  • Memory safety
  • Hygienic macros meta-programming (to extend the syntax of the language at will)

Rust has nice abstractions because it learns from other languages, especially functional ones. You can write elegant, concise code that does complicated stuff. Rust is an eclectic language.

It’s Fast

Compared to C, Rust is about 10% slower, similar to C++, and much faster than other languages like Java. Why not use C if you want performance, then? Because apart from being the king of speed, it’s also the king of bugs! In the case of other popular languages like Python, Rust programs run up to 20 times faster, according to the Programming Language Benchmark.

The speed of Rust comes from having a good optimizer, compiling to native code and many zero-cost abstractions, like iterators, which give the developer sweet syntax for free. No runtime costs are implied.

One exciting and unique way Rust achieves its speed and reliability is by ditching the garbage collector. This helps in two ways: more speed and more reliability. This is because when you have a garbage collector, there are no guarantees that it will free the memory. When it is freed, it happens at a random time. In addition, the garbage collector is an extra program that has to stop your code, do the cleaning, and then continue.

Rust can remove the GC by using stricter rules about the lifetime of data and how to drop memory. In Rust, memory is dropped when a variable goes out of scope. If you need something different, you need to tell that to the compiler through a thing called lifetime annotations. This makes you aware of how data lives through your program’s execution.

It Has Excellent Tools

Besides the abstractions, Rust has excellent tools that give you a modern and well-integrated experience while working on a project.

When working with Rust, Cargo, its project manager, is usually used. It is well-designed, has many features and can be extended easily. Cargo helps you create a new project, manage compilation targets, run tests, generate documentation and so on.

Thanks to the type system and thorough compiler, editors give awesome hints while coding. There are advanced plugins for the most popular integrated development environments (IDEs), like the Rust plugin for JetBrains IDEs, and RustAnalyzer for Visual Studio Code and NeoVim. In terms of code quality and standards, you get easy, automated formatting with rustfmt and extra linting with Clippy. This leaves out any potential dispute about style, so you can stay friends with your coworkers!

Another cool thing is that Rust code plays nicely with GitHub copilot and other AI autocompletion tools, so much of the verbosity of the language or boilerplate doesn’t need to be typed if you are OK with some AI code, of course.

It’s Versatile and Ever Evolving

Though Rust is known as a language for writing systems software only, it’s actually more versatile and general purpose. It can compile to embedded devices, to x86 ARM, and thanks to technologies like WebAssembly, Rust can be used for frontend development in a browser

The ecosystem is growing, and Rust is used for things like games, microservices, frontend, blockchain, CLIs, GUIs and so on.

Another exciting thing is that, even with this fast-paced evolution of the language, you don’t have problems with compatibility between versions. Rust guarantees the compatibility of a project with dependencies that run on older or newer versions of the compiler. That removes a lot of the hassle of dependency management.

Cool Stuff Is Being Made with Rust!

Due to the industry’s need for a programming language like Rust and its cool features, big companies have started using it. Among them are Meta, Microsoft, Google, Amazon, Mozilla, JFrog, and now even Infobip!

Apart from that, essential projects are embracing the language. Among the most notable is the Linux kernel. After more than 30 years, Linus Torvalds has been convinced to include a second language, apart from C, to develop the kernel. If that’s not recognition enough, I don’t know what is.

Additionally, many interesting projects sprouted, aiming to enhance the safety and speed of the overall software industry. Some of them are Dino, the evolution of Node.js, and WASI, which allows you to run general programs in WebAssembly with access to operating system resources. Cloud native systems are starting to benefit greatly from the minimal performance impact and safety that WebAssembly and WASI bring to the table.

Rust is also well suited for blockchain systems, and many of them use it, including Solana and DFinity’s Internet Computer. Another positive factor about Rust is that you get to work on cool projects!

It Makes Us Write Safer Code

The main way in which Rust achieves safety is by giving your code more order. In other languages, for example, you could have a variable passed as a reference to different functions, which can modify its value. That may have side effects because you could have multiple functions writing and several others reading, which may cause difficult-to-track bugs.

In contrast, Rust is strict in how you can pass values to functions, so they aren’t written by multiple functions. It also promotes immutability, which means the language itself prefers you to have values that cannot be changed, to remove nasty surprises.

Another way in which Rust achieves safer software is by making you aware of what can fail. You have to handle cases of failure. Otherwise, your code looks ugly, with many unwraps(). When a function can fail, you must be explicit about handling the case of success and the possibility of failure by wrapping the result of that function in a Result<T, E> enum. That enum has two possible elements, a success type and a failure type. This is just like Schrödinger box. If you had a function to buy a cat online, the cat might arrive just fine, or it might be dead. In Rust, you must write code to handle the two cases, making your program more robust.

It Has a Bright Future

Mainly, Rust is relevant because it comes with excellent tools and a modern experience, making more people use it.

The language also offers unique benefits that are very valuable to the industry. In the past, we had other languages with similar offers, but they lacked the characteristics that may make Rust a dominant language in the future. C, for example, is a bit faster but very error-prone. Haskell has nicer abstractions but never got widely adopted and doesn’t have the same ecosystem of tools. Other languages, like APL, had much cleaner syntax but were difficult to read and didn’t have a big community.

There were other, possibly safer, languages like SPARK Ada. Still, the same problems plagued them: They didn’t have good adoption, the same performance, or a great ecosystem of tools, and there was only a tiny community behind them.

The Rust community, on the other hand, is great, active and growing rapidly. Because of that, it’s easy to find help online, in official forums, Discord servers and documentation.

I am a proud member of that community, and I like to advocate for Rust because I believe it presents a real opportunity for the software industry to improve. We can have a safer and faster world of computing.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Pragma, JFrog.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.