Blog Insights A guide to Rust programming language
Published on July 21, 2020
8 min read

A guide to Rust programming language

Rust is a well-loved programming language but it is a mindset shift from options like C++. Here's a tutorial and an inside look at Rust code and its capabilities.

rust.jpg

What is Rust?

Rust is an open source programming language that has been the "most loved language" on developer community Stack Overflow's annual survey for the last four years. While it's a popular language in that sense, only a very, very small number of developers actually use Rust language today – a July 2020 look at the PYPL PopularitY of Programming Languages Index ranks it at number 18 with just .81% interest. (For comparison Python is at nearly 32% and Java is over 17%.)

So why the intense love of the Rust programming language? To put it simply, Rust coding was created to solve problems present in other languages and if you can take the time to unlock its (admittedly difficult) secrets, you're rewarded with cleaner, faster, and most importantly, safer code. Rust code resolves pain points that you see in countless other programming languages with far fewer downsides. Utilizing Rust allows developers to decide when they no longer need memory at the time of compilation which creates more efficiency around memory usage.

Antony Saba, a senior security engineer with Strategic Security at GitLab, recently talked about Rust during a company-wide series of meetings (Contribute 2020). He speaks from experience as his last employer was a Rust-based company. "Okay, so what's Rust's promise?" Saba asked. "Rust's promise is that it should be easier, and everybody should be able to fearlessly write at a systems level and not have to worry about memory safety or thread safety, or at least worry about it in the way that is supported by the language and the tools."

Let's unpack what that means.

History of Rust programming language

The open source Rust community describes the language as fast, reliable and productive. "Hundreds of companies around the world are using Rust in production for fast, low-resource cross-platform solutions," the organization says. Firefox and DropBox are two well-known users of Rust today, and Mozilla (creator of Firefox) was the first original supporter of Rust.

Who created Rust?

Rust code was originally developed as an open source project by software developer Graydon Hoare while working at Mozilla Research in 2006 and has been maintained by the Rust Foundation since 2021. It’s now one of the top drivers of the Rust programming language.

Think of Rust as the answer to a data-rich problem that will likely need lots of computational cycles. Mozilla's Rust documentation specifically calls out the language as ideal for "game engines, operating systems, file systems, browser components and simulation engines for virtual reality."

Benefits of programming in Rust

The top benefit of Rust coding is its adept memory management. Although there are other programming languages that emphasize memory safety like Rust code, Rust handles the concept differently in that it doesn’t use a garbage collector as other programming languages do. Instead, Rust uses a borrow checker to track variable scope and object lifetime while simultaneously administering high-quality memory safety and stopping concurrent data races.

The benefits of programing in Rust don’t stop at memory management. It’s fast and reliable for creating web apps and creating cross-platform applications, and it can integrate with preexisting code.

One of the other major benefits of Rust programming language is that it is well-suited for projects that demand extremely high performance. Its ability to process large amounts of data and CPU-intensive operations makes it a strong competitor in the developer space.

Other Rust feature benefits offer a list of features that makes it stand out from other programming languages. Here are some of the features:

  1. It’s more user-friendly.
  2. You’ll find high-quality documentation about the language.
  3. It has a better resolution of memory errors and concurrent programs than C and C++ languages.
  4. It’s incredibly fast and highly secure compared to other languages.

The Rust ecosystem

The JetBrains 2021 Developer Ecosystem Report found that Rust developers have mostly been using it for less than six months, and often reach for the language for "hobby" or personal projects. What are devs primarily writing with Rust? The report found command line interface tools, systems programming and web development were the most popular options.

Many companies have started using Rust, though. In 2020, Discord switched from Go to Rust, and Shopify, Dropbox, AWS and many others use it as well.

The basics of Rust programming language

Rust is a bit of a hybrid, according to Mozilla's Rust documentation. Rust offers developers the syntax advantages of high-level languages with the "control and performance of a low-level language," the documentation explains.

Rust is a statically typed language rather than a dynamic one. Though developers like to argue the merits of both, Rust, like popular TypeScript, eliminates the frustration of "dynamic typing." Data is constrained and checked by a compiler so confusion is minimized. Rust programming also makes it very hard to ignore errors – Steve Donovan, author of "A Gentle Guide to Rust," jokes it can be hard not to think the compiler is shouting at you when you make a mistake.

Donovan identifies Rust's key principles as:

  • Strictly enforcing safe borrowing of data
  • Functions, methods, and closures to operate on data
  • Tuples, structs, and enums to aggregate data
  • Pattern matching to select and destructure data
  • Traits to define behaviour on data

Types of Rust coding

Rust treats values by breaking them down into "types" in order to handle the data appropriately. According to MIT's guide to Rust, there are a number of types that can be split into scalar or integer types. Scalar types will likely be familiar to those who work with other programming languages: characters, Booleans, floating-point numbers and integers. They all represent a single value. Compound types are what they sound like – multiple types together.

Who uses Rust?

All of the guardrails mentioned lead to a language that can create fast-moving code with few things that slow it down. There's no runtime or garbage collection, making coding in Rust ideal for applications where memory usage is at a premium (like embedded devices). But if there is a place where Rust really stands out, it's security. Donovan points out that Rust is "safe by default," unlike C or C++. No one can corrupt memory by default, he writes.

The Rust programming language and productive coding

After three years of coding in Rust, Antony was quick to say he's probably more productive with the language than any other.

"I really do feel like Rust was the most productive language I've ever used," he says. "Once you are doing everything in that functional style, you're writing less code, but it's still clearer, because you don't have temporary variables. They're a thing that you don't really end up using when you're writing code that way. So, to me, it's those little things that I get the productivity out of."

Rust can be touchy, but rewarding

Productive, sure, but there's a learning curve with Rust.

"It's true the borrow checker is the hardest part," Antony says. "But the thing is, once you get past that, there is a serious dopamine hit when that program compiles, because it means now you only have your own logic errors to deal with. Part of that pain is explicitly some of the things that you assume, and some of the little white lies that you tell yourself when you're starting, especially with a C program. Because when you start your C program, it's like, 'all right, I have a couple command line parameters, I don't really want to write all my functions just to pass them, so I'm just going to declare a couple global variables and shove them in there, and I'll clean it up later.' Right? It's one of those little lies we tell ourselves. But you can't have immutable global variables in Rust. It just won't let you. You have to wrap it – you may as well just do the functions right. They're going to use your command-line arguments. It's the same with thread safety. You kind of have to do that upfront, and you don't get to make that assumption."

Looking to the future of the Rust programming language

Rust has a bright future, even if it might not be as widespread as other languages, Antony explains. "I don't think it's ever going to be as popular as Go, just because Google is Google, and there's a lot of places that Go is really good for," he says. "But for those places where you really want that fearless development, I think it'll continue to have a strong hold there."

Watch Antony's Rust demo in full here:

Read more about programming languages:

Can we solve the COBOL programmer shortage?

Why we use Ruby on Rails to build GitLab

How Modern C and C ++ work

Cover image by Zsolt Palatinus on Unsplash

We want to hear from you

Enjoyed reading this blog post or have questions or feedback? Share your thoughts by creating a new topic in the GitLab community forum. Share your feedback

Ready to get started?

See what your team could do with a unified DevSecOps Platform.

Get free trial

New to GitLab and not sure where to start?

Get started guide

Learn about what GitLab can do for your team

Talk to an expert