Rust is a fast, reliable, and memory-efficient programming language. It's been voted the most loved programming language six years in a row (survey). Created by Mozilla, it's now used at Facebook, Apple, Amazon, Microsoft, and Google for systems infrastructure, encryption, virtualization, and more low-level programming.
Note: This post is also available in Chinese, Portuguese, Vietnamese, and Arabic.
What is Rust?
Rust helps developers write fast software that's memory-efficient. It's a modern replacement for languages like C++ or C with a focus on code safety and concise syntax.
With Rust, developers have more control over memory allocation, without it being as painful as C++.
Rust uses a relatively unique memory management approach that incorporates the idea of memory “ownership”. Basically, Rust keeps track of who can read and write to memory. It knows when the program is using memory and immediately frees the memory once it is no longer needed. It enforces memory rules at compile time, making it virtually impossible to have runtime memory bugs. You do not need to manually keep track of memory. The compiler takes care of it. – Discord
On top of the companies mentioned above, Rust is also being used for popular open-source libraries like:
Rust has been a force multiplier for our team, and betting on Rust was one of the best decisions we made. More than performance, its ergonomics and focus on correctness has helped us tame sync's complexity. We can encode complex invariants about our system in the type system and have the compiler check them for us. – Dropbox
- Terser: developers wanted to generate the smallest possible file sizes.
- Prettier: developers wanted an opinionated code formatter that just worked.
- ESLint: developers wanted to find issues with their code before deploying.
SWC, created in 2017, is an extensible Rust-based platform for the next generation of fast developer tools. It's used by tools like Next.js, Parcel, and Deno, as well as companies like Vercel, ByteDance, Tencent, Shopify, and more.
SWC can be used for compilation, minification, bundling, and more – and is designed to be extended. It's something you can call to perform code transformations (either built-in or custom). Running those transformations happens through higher-level tools like Next.js.
Deno's linter, code formatter, and docs generator are built using SWC.
The Rust version probably could be made to work at an equivalent speed with enough effort. But at a high level, Go was much more enjoyable to work with. This is a side project and it has to be fun for me to work on it. – Evan, Creator of esbuild (Source)
Some argue Rust could perform better, but both could achieve Evan's original goal of influencing the community:
Even with just basic optimization, Rust was able to outperform the hyper hand-tuned Go version. This is a huge testament to how easy it is to write efficient programs with Rust compared to the deep dive we had to do with Go. – Discord
Why rewrite everything, then?
Making the necessary modifications to Babel to allow for it to be a reliable base for other tools would have required changes to absolutely everything. The architecture is bound to the initial design choices I made in 2014 when I was learning about parsers, ASTs, and compilers. - Sebastian (Source)
Rome is currently written in TypeScript and runs on Node.js. But they're now working on rewriting in Rust using RSLint parser and their own visitor system for AST traversal.
Rust's integration with Node.js is better than other low-level languages.
napi-rs allows you to build pre-compiled Node.js add-ons with Rust. It provides an out-of-the-box solution for cross-compilation and publishing native binaries to NPM, without needing
You can build a Rust module that can be called directly from Node.js, without needing to create a child process like esbuild.
Rust + WebAssembly
WASM is definitely a lot faster than JS, but not quite native speed. In our tests, Parcel runs 10-20x slower when compiled to WASM than with native binaries. – Devon Govett
While WASM isn't the perfect solution yet, it can help developers create extremely fast web experiences. The Rust team is committed to a high-quality and cutting-edge WASM implementation. For developers, this means you could have the performance advantages of Rust (vs. Go) while still compiling for the web (using WASM).
Some early libraries and frameworks in this space:
It's Rust all the way down.
Why Not Rust?
Rust has a steep learning curve. It's a lower level of abstraction than what most web developers are used to.
Once you're on native code (through Rust, Go, Zig, or other low-level languages), the algorithms and data structures are more important than the language choice. It's not a silver bullet.
Rust makes you think about dimensions of your code that matter tremendously for systems programming. It makes you think about how memory is shared or copied. It makes you think about real but unlikely corner cases and make sure that they're handled. It helps you write code that's incredibly efficient in every possible way. – Tom MacWright (Source)
- Extensibility: SWC can be used as a Crate inside Next.js, without having to fork the library or workaround design constraints.
- Performance: We were able to achieve ~3x faster Fast Refresh and ~5x faster builds in Next.js by switching to SWC, with more room for optimization still in progress.
- WebAssembly: Rust's support for WASM is essential for supporting all possible platforms and taking Next.js development everywhere.
- Community: The Rust community and ecosystem are amazing and only growing.
It's not just Next.js adopting SWC, either:
- Deno's linter, code formatter, and docs generator are built using SWC.
- dprint, built on SWC, is a 30x faster code formatting replacement for Prettier.
- Parcel improved overall build performance by up to 10x with SWC.
Parcel uses SWC like a library. Before we used Babel's parser and custom transforms written in JS. Now, we use SWC's parser and custom transforms in Rust. This includes a full scope hoisting implementation, dependency collection, and more. It's similar in scope to how Deno built on top of SWC. – Devon Govett
It's early days for Rust – a few important pieces are still being figured out:
- Bundling: One interesting area of development is
swcpack, which is SWC's replacement for Webpack. It's still under development but could be very promising.
- WebAssembly: As mentioned above, the prospect of writing Rust and compiling to WASM is enticing, but there's still work to be done.
Update 2023: Turbopack has been released, the Rust-powered successor to Webpack.
That's the world I want to live (and develop) in.
Thanks to Devon Govett for reviewing an early draft of this article.