Oliver Jumpertz
hero image

An Introduction To Rust

Share this post to:

Rust has hit the top spot on Stackoverflow’s yearly developer survey as the most loved language for several years in a row now. This is more than enough to justify An Introduction To Rust.

Overall, Rust has gained quite some popularity with the community in recent years. The language is used by many big tech companies these days, and the ecosystem is rapidly expanding. While other languages like Go have gained more traction, even in systems programming, Rust has been used for high-profile products and services that often run unnoticed but provide critical infrastructure like AWS Lambda.


Table of Contents

Open Table of Contents

What Is Rust?

Rust is a systems programming language that is compiled to binary. It has no runtime and instead uses a concept called “borrow checking”. Developers don’t need to explicitly free memory, the compiler does it for them. Next to this, the language has many modern concepts built into it that usually make it a joy to work with.

The language itself is multi-paradigm, offering functional, generic, imperative, structured, and concurrent programming with a huge emphasis on performance, memory safety, and developer productivity.

A few of Rust’s core features are:

Next to this, Rust has no exceptions or null pointer issues. Instead, it uses its variadic types (enums) to create structures that help deal with the absence of values and the occurrence of errors (Option and Result).

A simple hello world in Rust looks like this:

fn main() {
println!("Hello World!");
}

Who Uses Rust?

There are quite a few companies that successfully use Rust in production. Some of them use Rust for backend services, others build infrastructure with it. The range of applications is broad and a nice cross-section of all possible use cases of Rust.

A few of the companies that currently use Rust in production (non-exhaustive) include:

and many more.

The list grows every day while more and more companies begin to understand the advantages Rust offers. This does not mean that all companies suddenly switch from languages they have years of experience in, but there are usually fields where Rust shines for greenfield projects.

AWS, for example, uses Rust to develop Firecracker, which is the underlying virtual machine manager for its Lambda runtime. Rust makes perfect sense there as it interfaces with the Linux kernel to create so-called Kernel-based Virtual Machines.

Dropbox uses Rust to develop Dropbox Capture, a new visual communication tool designed to make it easy for teams to asynchronously share their work using screen recordings, video messages, screenshots, or GIFs.


Some Core Features Of Rust

Rust has many features, but some of them stand out and deserve a separate mention here:

Compilation to binary

Rust compiles to binary. When building your software, the compiler does everything necessary to ensure that you get a binary file at the end of the process. This binary isn’t platform-independent, but cross-compilation is possible.

In fact, the Rust compiler is a frontend sitting on top of LLVM. This means that the Rust compiler emits LLVM bit code that LLVM, in return, uses to create machine code for your target platform.

Memory-safety

The compiler ensures that no code you write can ever create a memory leak. It thoroughly checks your code, inserts free statements, and aborts the compilation if your code creates a leak.

The aim for memory safety goes even so far that the compiler doesn’t allow scenarios where it is unsure if there is really no way a certain piece of code could ever leak. Such cases are strictly researched before they are finally allowed by the compiler in a new version of Rust.

On the other hand, with unsafe code blocks, developers can instruct the compiler to ignore some of its safety guarantees. This comes in handy when memory needs to be leaked on purpose (like for Foreign Function Interfaces, FFI).

Performance

Rust is fast. It’s sometimes faster than C++ and C and sometimes slower. It usually depends on the scenario you benchmark the language in. On average, you can place Rust in one bracket with C and C++.

This doesn’t mean that all Rust programs are automatically fast, though. They are just (usually) faster if unoptimized than many comparable programs implemented in other languages. However, there are still scenarios where developers have to put more thought into their code if they want to squeeze out every last bit of performance they can get.

Developer-Productivity

Rust has an awesome toolchain. The compiler has some of the best error messages you have ever seen.

The language comes with an integrated package manager and build tool (Cargo), an auto-formatter (bye bye style-guide wars), and an impressive language server that makes integration into IDEs and editors (hello VSCode) pretty good.

Resource-efficiency

Rust has no runtime and, thus, no garbage collector. This means that memory does not grow and grow until a limit is hit and the garbage collector kicks in. It only uses what is allocated at a given time.

The resulting binaries are also pretty CPU-saving. This is bare-metal software running that contains binary instructions.


What You Can Use Rust For

Rust can be used for a multitude of use cases, and here are some of them:

Backend Development

Many frameworks enable you to develop microservices in Rust that use gRPC, HTTP REST, or GraphQL for communication, including async DB drivers and at least one fully-fledged ORM.

Rust’s safety guarantees and performance are a good choice for backend services that need to serve a few more users.

Creating CLIs

Creating CLIs is pretty straightforward in Rust. There are enough libraries for command-line argument parsing, and the language is powerful enough to support nearly all use cases.

As it compiles to one binary, distribution is okayish, but sometimes takes a little more work to get cross-compilation really working.

Embedded Programming

Rust compiles to binary and can run on a multitude of CPU architectures. Especially Rust’s memory safety, small binary size, and resource-saving nature make it a perfect fit for environments with limited resources.

Networking

Rust binaries are small and performant. Linkerd proxies are, e.g., written in Rust to offer maximum performance. As a bare metal language, a developer can use every native functionality of a host system without much overhead.

Web Development (through WebAssembly)

Rust supports compilation to WebAssembly. And this works out pretty well. The automatic memory management that the compiler brings to the table serves WebAssembly well and results in well-performing programs.

OS development

You can use Rust to develop operating systems, like you could do with C and C++. You may still write some drivers in C or Assembly, but Rust is a good fit for low-level stuff that is used by many higher-level functionalities.

Microsoft currently experiments with Rust to rewrite core functionality in Windows, for example, and Linux has recently merged basic Rust support for kernel development.


Rust Has A Steep Learning Curve

The language is awesome, but it is a huge shift from what most developers are used to.

As a developer, you really have to think about what you actually do with your variables, where you pass them as an argument, and how you reuse them after that. Garbage-collected languages remove the need to think about this from developers. The runtime and GC (garbage collector) simply take care of all memory management.

Learning how to really deal with variable lifetimes can take some time. But gladly, the compiler has some of the most helpful error messages throughout all compilers that really help you understand what you did wrong and where. This actually leads to you not having to google everything. And with time, you get used to it.


Conclusion

Rust is a really great language, and although Go has gained way more traction in recent years, Rust seems only to have walked a few steps back to run with an even faster speed afterward.

Rust will most likely be used in more and more scenarios as software engineers begin to understand more about its advantages and disadvantages.

Since Mozilla’s large layoffs due to COVID-19 and a restructuring, Amazon (AWS) has jumped in and shown huge support for the language. You don’t need to worry that it will go away anytime soon.


Share this post to: