Why I love Rust
I spent much of my free time over the past year learning Rust, and while it’s been a difficult language to fully grasp (it’s still a work in progress), I find it incredibly rewarding to write in. I also have had many conversations with people who don’t know much about Rust and are curious about the problems it solves.
This is my take on why Rust is important, and why I have fallen in love with the language.
Why Rust is Important
There are a handful of reasons that I find Rust to be an important programming language:
- It’s a community driven language - Rather than being owned by an organization (like Java) or single person (like Ruby or Rails), Rust is a community guided language. While I think that’s cool, what’s really exciting for me is how involved the Rust team is in getting us interested in the language and involved in contributing to it.
- It’s a different and more approachable take on a systems language - Systems languages come with a notoriety of being scary and difficult. Rust breaks away from that, and the Rust team has worked hard to make Rust much more approachable including writing The Rust Programming Language book.
- It’s a powerful language for solving problems - If you’re coming to build command line tools, web server, or blog, Rust is an incredibly powerful language to use. I describe why in the rest of this blog.
All of that is awesome. But if I were to describe Rust’s superpower, it’s this: Rust allows you to write highly performant code without leaving yourself open to security vulnerabilities. The key ideas here are “performance” and “safety”.
What does performance mean?
Performance can mean many things, but here we’re specifically talking about computation speed and memory usage. When it comes to speed and memory, Rust joins the ranks of C and C++. All three languages have similar attributes that give it the ability to perform stronger than others, such as not having a runtime (or being garbage collected) and allowing developers to write more low level code. But with C and C++, performance comes with the cost of potentially unsafe code.
What does it mean to be safe?
“[Rust is] targeting all of those bitter C++ programmers who are sick and tire of writing security holes” - Jim Blandy, author of Programming Rust
There are many factors that make C and C++ unsafe:
- Manual memory management: i.e. having to free the memory yourself. Forgetting to do so or doing so too early can lead to problems.
- Unrestricted access through pointers: while pointers are not necessarily unsafe, having the ability to read from memory that may have changed can lead to data races, and unintentionally allowing pointers to reach parts of memory that they shouldn’t have access to is definitely unsafe.
These factors among others can lead to dangerous vulnerabilities, ones that we have had for decades and will continue to have for many more. It’s these factors that make the languages unsafe.
Rust handles things differently. The creators of Rust realized that if we follow a few simple rules, Rust can automatically manage memory with out garbage collection and prevent data races. It’s these rules that allow Rust to bypass the vulnerabilities of C and C++:
- Each value in Rust has a variable that’s called its owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped.
The Rust Programming Language, Chapter 4: Understanding Ownership
Rust builds upon these rules with features like the ability to borrow or share values with references, or the ability to share behaviors with traits, among many others.
Abstraction without a penalty
Rust also has the ability to keep its performance while having higher level abstractions like functional methods and shared or generic behaviors. Typically, even in languages like C++ these types of features come with a cost at runtime, but thanks to Rust’s wonderful compiler, Rust doesn’t charge for readability.
Side question I get a lot - Is Rust like Go?
Not really. The two languages are tackling different problems. I have seen Go tackling projects that would have previously been built with Java or a similar language. While I don’t have a lot of experience with Go, it seems to be great for projects that are more IO bound. These types of projects don’t seem to be the pursuit of Rust (although, there are a few projects moving in the IO direction). Again, Rust is going after the realm of C and C++. It’s regaining your power over your memory and CPU.
Why I love Rust
There are many reasons why I love Rust and why it’s my go to language for hobby projects. These reasons stand out the most.
It has introduced me to an unfamiliar domain - Systems Programming
The idea of doing systems programming for me has always been behind of the wall of having to write C or C++, and that didn’t appeal enough to me to invest my time in. Now that I have found a love for Rust, a whole new world of programming has opened up for me.
It is challenging the way I write code
Writing Rust is very different than writing Ruby or Java. Initially, writing Rust came with a lot of gotchas for me. I’ve had many arguments with the compiler over changing immutable variables or changing a variable while it’s being held by a reference. It was a little frustrating, but the Rust Compiler offered useful guidance to ease the pain.
Over time, I have found myself thinking about the code I’m writing differently and I struggle less with the compiler. The concepts of Rust have started to become common sense, and it has changed the way I think about writing code in Ruby. That effect makes me enjoy writing in Rust even more.
Its community is strong and kind
I love the kindness and openness of the Ruby community, and it’s part of what helped me fall in love with Ruby. Communities and the social aspect of programming are important to a language; it’s a signal of the type of people you will interact with day to day. This is proven by looking at how we spend our days: reading blogs and documentation, searching for questions we need answered, keeping up with what’s going on in the industry. Understanding that, our time is not worth being spent in a toxic community. That’s why enforcement of Codes of Conduct is so important.
From my experience, Rust takes enforcing the code of conduct seriously. The result has been a diverse community of kind people who are open to help and collaborate.
Who’s Using Rust
Mozilla - Firefox
Mozilla is the major sponsor behind Rust, and they are using it to rebuild parts of Firefox. Using Rust allows them to have better parallelism, performance, and security. Here’s a great blog from Mozilla on incorporating Rust into Firefox. And here’s another great blog on how they rewrote the CSS engine in Rust with massive improvements.
Dropbox - Compression
Dropbox recently blogged about a new compression algorithm that they have been building using Rust.
Facebook - Source Control
Facebook is using Rust to build an experimental source control system, and it’s being specifically designed to deal with large repos.
Facebook’s Source Control team is seeking an experienced software engineer with expertise in Rust or a similar systems programming language, to build and improve our next generation source control server.
Facebook Job Posting
Chucklefish - Developer behind Stardew Valley
So, project Spellbound finally has a proper name... pic.twitter.com/4UwUy5rpH0
— Tiy (@Tiyuri) March 14, 2018
If you loved Stardew Valley, get excited for Chucklefish’s next game WitchBrook! It looks awesome, and it seems to be written with Rust.
I’ve followed along with game development using Rust, and it’s mostly been proof of concepts up to this point. It’s exciting to see an actual game coming out, and I know more will lead the way as Rust tooling and Crates mature for games.
Microsoft - Azure IoT functionality
Microsoft is using Rust for its IoT Edge Security Daemon. “The daemon provides an abstraction over Hardware Security Modules (HSMs), enabling secure SAS token and certificate provisioning for IoT Edge modules.”
Lots of others
You can see how even more companies are using Rust on the Friends of Rust page. The list continues to grow every month.
Where to learn about Rust
Getting Started / Books
The best resource for getting started is the official Rust book, The Rust Programming Language. It’s a comprehensive book that will ease you into the many features of Rust.
I have also enjoyed the Programming Rust book. It’s much more in depth, including details about the memory layout of Rust features. Parts of it read like documentation.
Podcasts
The author of Programming Rust was on the Co-Recursive podcast twice speaking about Rust, and they have been extremely informative. Here’s Part One, and here’s Part Two.
If you want a great Rust podcast, check out the New Rustacean. The podcast host, Chris Krycho puts a lot of time and research into his episdoes, and they show.
Another good podcast is the Rusty Spike which gives a weekly 5 minute update to things happening in the Rust world. Thank you Jonathan Turner for keeping us up to date!
Lastly, Sean Griffin and Derek Prior often talk about Rust on episodes of The Bike Shed. Sean started and maintains a database ORM written in Rust called Diesel. Sean is who inspired me to get into Rust, and listening to him talk about working on Diesel is pretty fun.
Newsletter
Lastly, keep up to date with Rust resources through the weekly newsletter, This Week in Rust.
Something Fun: Test the power of Rust
If you want to see how powerful Rust can be without having to learn it, try out ripgrep, a command line search tool. I’ve used it to search GB’s of log data, and searches that typically take minutes can happen in seconds with ripgrep.