Why we have been adopting Rust for the backend.
TABLE OF CONTENTS
Embracing Rust for web development isn't the conventional path, especially for an outsourcing team that often prioritizes rapid iterations over scalability like SENS
Rust is renowned for its challenging learning curve and time-consuming adoption process.?
So, why are we wholeheartedly considering Rust?
1. What we don't like about TypeScript
I might say “We all love TypeScript”. Some love the DX that TypeScript brings, some others just love tweaking around with TypeScript to make other’s experiences even better.
But at the end of the day, it’s still JavaScript. Even if I love the DX, I still think JS is not the right one for the server.?
To begin, Node.js operates as a single-threaded runtime. While some believe that non-blocking I/O and asynchronous architecture suffice, this is not the case for several reasons.?
Non-blocking IO means IO-related tasks like network calls will not block other tasks. Meanwhile, computational tasks still block everything else. For short, multithreads allow computing in parallel and async allow waiting in parallel.
This limitation becomes significant when an outsourcing team places excessive focus on a language that is not known for speed and can be computationally blocking.
Secondly, Node.js is notorious for memory leaks. And these kinds of bugs are so hard to find. Skill issue, we’ve agreed to that.
Although TypeScript eases a lot about type system, it does nothing about error. Handling errors can be easily overlooked.?
Configuring TypesSript can be a nightmare, particularly when integrating it with tools like Webpack or Esbuild and dealing with libraries that support only ECMAScript modules (ESM) or CommonJS (CJS).
TypeScript is like a Swiss Army Knife. So cool, so useful and so easy to accidentally cut my own fingers.
We love JavaScript, and we also love its memes.
2. Why not all in Golang?
That’s our initial plan. Golang seems flawless at first.?
Go is fast, Go is simple, Go’s APIs for handling parallel and concurrent tasks are so elegant. It also treats errors as data, which I think is so good.
People are adopting Go everywhere from webs, apps to devops or cloud. It means the community is growing fast, and there will be lots of libraries (and jobs ??) for us.
In fact, we’ve made internal tools that convert all Golang API’s schema to Typescript for replacing tRPC. And we are also building our internal platform with Go.
Okay, it's time to complain!
Golang is super verbose and lengthy. Codebase is so imperative, both coding and reviewing are so boring and tiring for me.
Why are some useful and simple things like enum or tuple not supported? Why do I have to handle so many errors? Why do I have to write my own iterator for map or filter? Why are Go's community documents so ugly and missing details?
Lots of one-line-logic from Javascript become many-lines-logic in Go. Generics is also limited and tedious to work with compared with typescript.
Somehow, the simplicity that I’ve craved for becoming what I hate the most. I know I know, skill issue again.
I think Golang is like a wooden sword. And only seasoned swordsmans who have attained the essence of the way of the sword can use it with joy. I’m still young and have a long way to go. I don't want to stick with a dull and tedious wooden sword just because it's safe and easy to play with.
领英推荐
3. Time to get Rusty.
I started learning Rust a year ago because my boss (a non-tech guy) liked it and wanted me to adopt it (I have no idea why ??).?
And yes, I quit twice in a month because Rust is too hard.?
But after a while struggling playing with Typescript and Golang, I miss something that I’ve learned in Rust - a type system. And this time of learning, Rust makes so much sense.
Rust is difficult, but not that much, just a bit of mental shift to understand how Rust helps to manage memory and everything will become more joyful.
Instead of craving for simplicity, I now long for a stringent mentor that prevents me from making critical mistakes and refines my thought process. And Rust’s compiler is exactly what I've been searching for.
Rust is the one systems programming language that I know looks really like a high level programming language. Its syntax is so concise and elegant. Many logics implemented in Rust are even shorter than in TypeScript let alone Golang.
And my favorite part, THE TYPE SYSTEM.
Handling state/data is the most difficult thing in programming (I guess). And the ability to model data in a sophisticated way reduces difficulties of handling by a lot.
Rust supports algebraic data type, best enum in the world and even monad (without calling it monad). Generic and trait systems are also surprisingly powerful.?
While in TypeScript or Go, I grapple with creating a data model for managing a business flow, in Rust, the challenge lies in selecting from numerous better options swirling in my mind.
And macros for metaprogramming, it opens a huge opportunity for the community. Let’s take a look at https://leptos.dev/ . A frontend library written in Rust that looks exactly like React (Solidjs actually). That kind of syntax is just unachievable in Go.
And things that I care least about, memory safety and performance. Because everyone knows it, this is a low level language, low memory footprint and blazingly fast is a must. But the cool part is zero cost abstraction, which means we don’t have to pay a cost of performance or memory for using high level syntax.
Let’s look at the benchmark below, other languages may cost up to 100 times for using iterators whereas Rust not only matches but exceeds the performance of a simple loop. It is completely absurd! If you confuse “how about Go”? Huhm, Go doesn’t support it.
Learning Rust is also enjoyable because of its documents and books. The template is so elegant, code is highlighted and executable right on the web. And everything is written with great attention to detail.
Rust is not just a tool but a guiding spirit, wielding that tool at my command. Changing from using sword to word requires significant mental adjustments, but it’s highly efficient and safe.
If you still think that I’m biased, let’s take a look at stack overflow’s survey about the most loved language.
Rust quietly became a cornerstone of modern JavaScript tools. Turbopack, Vite, Parcel, Rspack, Tailwind, and more are all choosing to write parts of their JavaScript-focused tools in Rust.
And I firmly believe that the trend of "rewriting in Rust" primarily stems from its ergonomic qualities, rather than being solely driven by performance or memory considerations.
The main barrier hindering a more widespread adoption of Rust is the state of its community. It's not about the availability of libraries or third-party support but the challenge of finding developers who are willing and ready to learn Rust or transitioning projects to a team proficient in the language.
I truly hope for a future where Rust enjoys not only the love it rightfully deserves but also the widespread recognition and adoption it has the potential to attain
Stay tuned for the next episode , where we will delve deeper into another technologies and its game-changing impact on development!
Writer: Hi?u Nguy?n - R&D Lead
Wording by Chat-GPT
Source: