DEV Community

Cover image for Rust đŸĻ€: Why JS devs are switching to it?
Bhavesh Yadav
Bhavesh Yadav

Posted on

Rust đŸĻ€: Why JS devs are switching to it?

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.


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


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! đŸĻ€


Top comments (11)

Collapse
 
sirajulm profile image
Sirajul Muneer

Click bait. You only specified features of Rust. You never mentioned why JS developers switching to Rust.

Collapse
 
codezera profile image
Bhavesh Yadav

Because of these features only JS devs are moving to rust 😅

Collapse
 
sirajulm profile image
Sirajul Muneer

I can say the same with any language. That clearl makes your article a click bait. Nothing worthy otherwise. Do you mean to say frontend developers are moving to Rust from typescript due to this feature? How would you run rust on a browser, other than webassembly which is still not mature enough?

I am a fan of Rust and dislike a lot of stuffs in TS. But your article is just rubbish and does not provide any reason why people are leaving Typescript for Rust. I can make 1000 articles just by replacing Typescript from your article and replacing it with any other language.

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
sirajulm profile image
Sirajul Muneer

So you say frontend developers are not JS developers? You should have then explicitly said, JS backend developers. I don’t know how much experience you have, but these kind of articles make you look like immature junior developer who try to make some visibility for himself. Bro, I can tell you Typescript is not going anywhere and no language is going anywhere. People have been telling this for ages even before you were born about many different languages. And they are still alive.

Try to be just to what you write, I will support you for every bit of information you write here to educate people the right way. Clickbaity articles are no way!

You can just replace typescript with any other language name and make it yours. I don’t want to create click baits like you do.

Collapse
 
thomashighbaugh profile image
Thomas Leon Highbaugh

Exactly the direction I have been exploring and the future if I was going to wager a bet on the course front-end development would take. Glad to see that for once, I am not alone in suspecting something not obvious to the majority. Cheers.

Collapse
 
codezera profile image
Bhavesh Yadav

In coding, you are never alone, the problems you face, the problems you solve all are done my many others in the exact same way😂

Collapse
 
mrdulin profile image
official_dulin

I can post another:

Go đŸĻĢ: Why JS devs are switching to it?

Collapse
 
artxe2 profile image
Yeom suyun

I expected an interesting post answering the question "Rust đŸĻ€: Why are JS devs switching to it?" but the actual content seems totally unrelated to the title.

The introduction of the article mentioned "some key concepts that every JavaScript developer should understand when learning Rust." This might have been a more appropriate title, and in this case, it would have been beneficial to include a link to one of Rust's significant advantages at excellent guide document.

Collapse
 
sirajulm profile image
Sirajul Muneer

This is just a clickbait bro. Replace typescript with any language, you can make so many articles. 😂

Collapse
 
codezera profile image
Bhavesh Yadav

Not sure why you didn't find it useful, i'll try to improve in future i learn every day thank you for your suggestion, i'll definitely take care of adding links in future