DEV Community

Manon
Manon

Posted on

From TypeScript to Rust: Navigating Memory Safety Challenges

I’m setting out on an exciting new challenge: learning Rust. Why? It’s the most loved programming language, there’s got to be a reason! Plus, I’ve designed a curriculum to help people level up their backend skills to land and work on more meaningful, impactful projects. I’m the first test subject. You can have a look and level up here.

Learning a new programming language isn’t magic.

It’s all about practice and tackling plenty of programming challenges 😊

For example, Leetcode is a solid place to start. Exercise two: Linked List. Even with five years as a developer, I had to refresh my memory. Working as a TypeScript/React/Node.js full-stack developer at startups, you don’t often encounter structures like linked lists.

Simply said, a linked list is a group of nodes where one node points to the next.

I often find it easier to think in analogy. So here's one: a linked list is like a hiking trail with markers in the mountains. Each trail marker (node) points to the next one. You can only move forward step by step, and can't take shortcuts! If you want to reach a specific marker, you have to follow the trail from the start. This makes it simple to add or remove markers (nodes) without messing up the entire trail.

Sometimes you need a structure that’s adaptable, like a trail that can grow or shrink without a complete redesign. Linked lists, for example, are great for apps that manage dynamic tasks, like your daily to-dos. When you add or complete a task, the list adjusts seamlessly, just like updating a trail (if this is a thing at all).

Implementing a Linked List in Rust, it turns out, is a tough one.

I found it harder in Rust compared to, let's say, Typescript, because of its strict ownership and borrowing rules, which are there to ensure memory safety. Each node needs to point to the next, and Rust's borrow checker ensures no mutable reference is active while others exist. In simpler terms, Rust needs to know exactly how much memory to allocate, and where. As a beginner with Rust, wrapping my head around these concepts has been fun.

TypeScript, on the other hand, makes this easier. It’s garbage-collected, so memory management is automatic. You can create nodes as objects and connect them without worrying about ownership, borrowing, or memory allocation. But that simplicity comes with trade-offs (of course): Rust’s strictness leads to better performance and safer code, even if it makes something as "basic" as a linked list harder.

How do we implement a linked list in Rust?

My understanding is that it has to be with dynamic memory allocation. Rust can do this with the <Box> smart pointer, which allocates memory to the heap at runtime instead of the stack. However, this approach introduces challenges, because heap memory isn’t automatically freed, which makes it less safe and contradicts Rust’s core principles.

I’m excited to dig deeper into these safeties issues, if the <Box> container is really the answer, and make a Linked List in Rust!

What about you? How would you approach building a linked list in Rust?

Top comments (0)