Rust 🦀: Why JS devs are switching to it?
Tags: javascript, rust, webdev, programming
Published: 2023-12-24 16:45:32

Are you a JavaScript or TypeScript developer curious about the programming language Rust 🦀? 

🤔 Well, you're not alone! Rust has gained significant popularity and has been the favorite programming language for a many devs over the years.

Moreover, Rust is now infiltrating the JavaScript ecosystem with projects like SWC, TurboPAC etc. Even existing tooling is transitioning to Rust, with utilites like Tailwind planning to utilize Rust for various components.

In this blog post, I'll walk you through some key concepts that every JavaScript developer should understand when learning Rust. 

> I'm also learning rust so if i'm wrong at any part so please correct me in the comments section :).

---

## Memory Management: Ownership and References

Memory management in Rust is one of its distinct features. While languages like JavaScript have garbage collectors that handle memory for you, Rust takes a different approach. 

Rust follows an ownership model, which gives you control over memory but with some guards to prevent common issues in memory management.

In Rust, every value has an owner, and there can only be one owner of each value at a time. When the owner goes out of scope, the value is dropped from memory. This ownership model ensures that memory utilization is efficient and prevents dangling pointers and memory leaks.

To work with values without changing their owners, Rust offers `references` and `borrowing`. Rust is explicit about borrowing a reference to a value, and you can choose between immutable borrowing (the value can't be changed) and mutable borrowing (the value can be changed).

In short, Rust enforces strict rules for borrowing to avoid data races and ensure memory safety. For understanding these concepts in details, read the docs [here](https://www.rust-lang.org/learn).

---

## Variables and Mutability

In Rust, variables are immutable by default, meaning they cannot be changed once assigned. To mutate a variable, you have to explicitly declare it as mutable using the `mut` keyword. This explicit mutability makes it clear which variables can be altered, reducing the likelihood of bugs.

In JavaScript, we are used to the flexibility of changing variables on the fly which is obviously easy but it may lead to many bugs.

---

## Rust is a Compiled Language

One of the first things to understand about Rust is that it is a compiled language. This means that there is no runtime that executes Rust code directly in the browser. If you're familiar with TypeScript, the process is similar. In TypeScript, you use Babel or other tools to transpile your code to vanilla JavaScript so that the browser can understand it. 

Similarly, in Rust, you need to compile your code into a performant executable or .exe file that runs on your system rather than the browser.

---

## Types and Type Inference

Rust is a static and strongly typed language, which means every value must have a declared or inferred type. Rust's strong typing enforces type safety during compile-time, reducing the chance of runtime errors.

Rust provides several options for declaring and inferring types, giving you greater control over memory usage and numeric precision (If you are familiar with c or c++ then you can relate it with this very easy). For instance, you can specify size-specific options for integers and floating-point numbers, which allows you to reserve memory based on your specific needs.

Unlike TypeScript, Rust has no `any` type or `null`. Rust encourages handling errors at compile time, ensuring robust error handling practices.

TypeScript devs be like: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5cvcx8s7ot55w6w167t.gif)

---

## Error Handling

Error handling is crucial in any programming language, and Rust places a strong emphasis on it. Rust categorizes errors into two types: `unrecoverable` and `recoverable`.

Unrecoverable errors are severe errors that should cause the program to stop immediately. Rust provides a `panic` macro to handle such errors, and it produces a trace for easy debugging.

On the other hand, recoverable errors are errors that can be handled gracefully. Rust offers tools like pattern matching and the `Result` type to handle recoverable errors. The `Result` type allows you to handle both successful outcomes (`Ok`) and error cases (`Err`) in a structured manner.

---

# Conclusion

So, are you ready to embark on your Rust journey and experience the power and safety it brings to your code? 

Let us know in the comments below! Till then

Happy coding! 🦀

---