An Introduction To Rust
Let's take a look at Rust. The most-loved language, according to StackOverflow's yearly developer survey that not enough people seem to use professionally.
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.
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.
Who Uses Rust?
There are actually quite a few companies now that successfully use Rust in production.
- Amazon Web Services (AWS)
and many more.
Some Core Features Of Rust
- Compilation to binary
Compilation to binary
Rust compiles to binary. On 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.
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 would create a leak.
The aim for memory-safety goes even so far, that the compiler doesn't even 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.
Rust is damn 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++.
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.
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
- Creating CLIs
- Embedded Programming
- Web Development (through WebAssembly)
- OS development
There is a multitude of frameworks that enable you to develop microservices in Rust which use gRPC or HTTP REST for communication, including async DB drivers and at least one fully-fledged ORM. Rust's performance makes services perform awesomely well.
Creating CLIs is pretty straight-forward in Rust. There are enough libraries for command-line argument parsing and the language itself is powerful enough to support nearly all use cases you could think of. As it compiles to one binary, distribution is easy.
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.
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 awesomely well. The automatic memory management that the compiler brings to the table serves WebAssembly extremely well and results in well-performing programs.
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 a lot of higher-level functionalities. Microsoft currently experiments with Rust to rewrite core functionality in Windows.
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 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.
Rust is a really great language, and although Go has gained way more traction in recent years, Rust seems to only have walked a few steps back, to run with even faster speed afterward.
I'm pretty sure that we will hear more and more about Rust being used in different scenarios and by many more companies (and also well-known ones).
Since Mozilla's large layoffs due to COVID-19 and a restructure, Amazon (AWS) has jumped in and shown huge support for the language. You don't need to worry that it goes away anytime soon.
Before You Leave
If you liked this article, feel free to visit me on Twitter. I regularly post content there. It is basically the platform where you will find my content first before it lands on my blog or somewhere else.