Why you, a JavaScript Developer, should learn Rust in 2022

This is why you, a JavaScript developer, should learn Rust in 2022.

1. Rust complements your skillset

Rust is a general purpose language, which means it overlaps a lot with JavaScript. This is great because it means we can use Rust in a lot of the same places we use JavaScript.

However!

Rust is not going to replace your Vue, React, or Svelte skills.

Rust is not going to replace JavaScript in the general browser use case.

Rust complements the skills you already have by enabling you to write high-level, performant, maintainable code in Serverless functions and CLI tools.

I’ve only mentioned a couple use cases that overlap with JavaScript here, we’ll cover more later.

2. An Integrated Toolset

You’ll feel right at home with Cargo, Rust’s package manager, which feels very familiar to using npm or yarn.

The biggest difference is that Cargo comes with many features we need third party packages for in JavaScript.

npm lets us initialize new projects, install binaries, run scripts and more recently: organize our code into workspaces. Cargo also has these.

The package.json and package-lock.json files are also mirrored by Cargo.toml and Cargo.lock.

This is where it starts to get interesting.

When we want to test our projects, we need a third party test runner, an assertions library, and maybe more in JavaScript. This is built in with Cargo and Rust.

Similarly for building apps and libraries, linting, auto-fixing lint rules, typechecking, generating documentation, and auto-formatting. With JavaScript we need a different package for each use case. In Rust we use Cargo.

Cargo and Rust also goes above and beyond that by including functionality like benchmarks.

NPM/Cargo cheatsheet

This integrated tooling has real impact on the day to day experience of writing Rust. Tests become something you can just do quickly as part of your development flow instead of something that requires the installation and setup of third party packages.

In JavaScript we have to pre-build and pre-bundle our libraries, leading to an explosion of complexity for publishing a package to NPM both in the compilation of that code and also the usage of the deployed artifacts. CommonJS vs ESM vs UMD, ES2020 vs JSX vs TSX, package.json fields that contain hints for bundlers, and more.

In Rust, the compiler knows how to compile different versions of the language per-crate. So a crate using Rust 2015 can be used in your Rust 2020 project with no issues.

3. Parts of the Rust language are leaking into JavaScript

While far from being the only influence on new features being added to JavaScript, Rust is often cited as Prior Art in tc39 proposals like Pattern Matching.

Learning a language these features are already in means that when they land in JavaScript, you’ll already be familiar with how to use them in a production setting and can start using them immediately.

This also extends to languages like TypeScript. Learning Rust, which has a type system built-in to the language, will give you more context on using TypeScript effectively. You will understand more about what tradeoffs TypeScript is making to accommodate the underlying JavaScript implementations and be able to make better use of them.

4. Parts of JavaScript are becoming Rust

These days JavaScript isn’t just the language, it’s the ecosystem of developer tools we use to produce better results for our users.

Mainstream tools like SWC, a babel competitor written in Rust, are becoming more and more common. NextJS, Parcel, Apollo GraphQL, and Relay are just a few of the major converts to Rust powered infrastructure.

While you will probably never have to know Rust to work with these tools, knowing Rust will give you an edge in understanding how these tools fail... and fixing them when they do.

TLDR

There are many other reasons to learn Rust, from the queer-friendly, welcoming community, to the friendly error messages, and yes, performant programs that use fewer system resources is easier.

During your journey you’ll hear a lot of people talk about memory safety, memory management, and other concepts that feel weird to talk about as a JavaScript developer. When do we hit a null pointer exception when writing JavaScript anyway? Understand that these talking points are important to people with different contexts, like C programmers, that aren’t using higher level languages and can easily run into different problems than we experience as JavaScript developers.

In the end, Rust is a high-level general purpose language that complements your existing JavaScript skills. While it does include some new concepts, such as Ownership, it empowers you to write code for high-level use cases like serverless functions as well as low-level use cases like operating systems all while feeling like you’re writing a language that quite frankly, feels like JavaScript to me.