Why JavaScript Devs are Switching to Rust in 2024

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Rust will make you hate JavaScript. When I was learning Rust, I couldn't help but notice the same comment everywhere by former JavaScript developers. And this video, I'll walk you through five key concepts you need to understand as a JavaScript or TypeScript developer learning rust. If you stick around to the end, I'll give you four foolproof steps to start learning Rust. Today, Rust has been the favorite programing language for a record eight years in a row in the StackOverflow survey, and Rust is also infiltrating the JavaScript ecosystem. We've got Turbo PAC, RS, PAC, SWC and Lightning success. Even existing tooling is moving to rust. Both Veit and Parcel are transitioning critical pieces to rust and Tailwind has planned to move its default parser to rust. Now aren't you curious? Could you learn Rust as a JavaScript developer? It might be easier than you think. Let's cover these five high level concepts one by one. I wish I would have known before learning Rust will mostly avoid syntax for now, but I think you'll find that if you understand these concepts, this syntax will come much easier to you. The first key concept is that rust is a compiled language. In other words, there's no runtime that executes Rust code. If you're familiar with TypeScript, it's a similar process. Since the browser can't read TypeScript. You use BABBEL or some other tool to transport all your code to vanilla JavaScript Rust doesn't target your browser, however, but your system. So you'll need to build your code to compile it to a performant executable before running it. Memory Management. How Rust manages Memory is one of its distinct features. Most languages either handle memory for you like JavaScript garbage collector, or give you complete control over memory management like C or C++. These two memory choices are opposite tradeoffs. Garbage collected languages are often less error prone, as you don't typically have to worry about seg faults. Buffer overflows and the like. But you sacrifice your ability to control allocation and the allocation of memory. Rust follows a third option called the ownership model, which gives you control over memory, but puts up guardrails to help you avoid many common pitfalls in memory management. You still manage the memory, but Rust checks your choices against guiding rules. If you violate a rule. Rust won't compile your program. So what are these rules? There are two essential ones. First, each value in rust has a variable that's called its owner. It's responsible for the value. And very importantly, there can only be one holder or owner of each value at a time. Here I have a string, Chris. Currently the variable name is the owner. I can pass that value to my function print string, but at that point the function now owns the value. And that brings us to the second rule. When the owner goes out of scope, the value is dropped from memory. I can't then access the list again in the main function. So name owns the string, then it's passed to print string, which is now the new owner. And finally, once it's printed, the string is removed from memory. Since its owner is out of scope JavaScript, the value would live on in memory until the garbage collector clears it, which can be ambiguous. In rust, it's very clear when the owner moves out of scope, the memory is dislocated. These rules are the special sauce to memory management in rust. Now I know what you're thinking. Wait a second. How can I ever pass any values without changing their owners? That's where references and borrowing enters. While values cannot have multiple owners. Functions and other calls can borrow references to values which belong to other owners. Again, here rust is explicit. You add an ampersand to a referenced value. We're getting a little ahead of ourselves, but borrowing a referencing of value can happen in two different ways. And here I think an example would help. If I let you borrow my car, I'd expect you to return it in the same condition that would be immutable borrowing. You can borrow it, but you can't change it. If you were my mechanic borrowing my car, though I may say, Hey, if you find anything you want to improve while you have it, go for it. That would be a mutable reference. In other words, you can change it with either type of reference. Though I'm still the owner and you're only borrowing in this case names and a mutable reference. I let the function use the string but is not allowed to change it in any way since the owner retains the value. That also means I can pass a reference twice in a row as the owner is still name. This brings us to variables and mutability and rust. All variables are immutable by default. In other words, they cannot be changed. To mutate a variable you have to explicitly marked as mutable with a mute keyword. So for instance, if you wanted to pass a mutable reference to name, you'd add mute and now the references mutable meaning that print string can change the value while it's borrowing it. And since the variable name still owns the string, I can pass the mutable reference to print string a few times in a row. In practice, let by itself works like constant JavaScript while let mute works like let and JavaScript. Since rust is explicit, it's easy to tell whether or not something can be mutated. It's not dependent on the type, like if it's an object, it's if it has that mute keyword. So here count cannot be modified until I add the mute keyword. As a JavaScript developer, you may be a bit claustrophobic right now. Hang in there. We're used to changing around nearly any variable, but that flexibility introduces a lot of bugs. For example, if you pop the last item from an array, you've also mutated the array in place and you just have to know which methods mutate in which methods copy. Here we've mixed a specific value. The last item in the original array with a mutated value, the altered array Does last item mean the last item in the original array or the mutated one? You'll have to manually track down the reference in JavaScript and rust, though we'll see two differences. First, you have to mark the array with a mute keyword. Only then can you mutate its values. But the changes is explicit and clear. The second difference is that you cannot mix mutable and in mutable references. You can either have a single mutable reference or any number of immutable references. For this reason, some rust developers call references exclusive mutable and shared immutable. That was a lot. Mutability References Ownership Compiling. Let's take a quick break and look at a few things that will look very familiar to you as a JavaScript developer. When rust was being developed, they built its package manager cargo based on the best parts about NPM. So it feels very much like AMPM, but with superpowers, it handles packages, test running, compiling and more. There are lots of other similarities too. Instead of a package that JSON file, you have a cargo dot Tamil file. Instead of packages, you have crates and you can explore all 130,000 plus of them at Crate Style. An online registry like NPM Jasco. But you'll notice a lot of other similarities, like the comments syntax, the structuring, async await imports, exports and more. Let's jump back into the overview by looking at this fourth key concept types. Now, if you're familiar with TypeScript, this is going to look very similar in syntax. However, Rust gives you way more options and actually enforces the types. It's a static and strongly typed language in plain English. This means every value must have a type either inferred or declared, and that type is enforced at compile time. Sorry, you can't just coerce numbers into strings whenever you feel like it without explicitly marking that change. Like TypeScript. Rust can often infer types for you, for instance. It can infer that my NUM is an I30 too, and my name is a string slice. While using the inferred types is often sufficient, you can provide more direction with an explicit declaration in rust gives you lots of options. You don't need to know them all by heart when you first start learning. But as a brief example, instead of the single number in typescript, Rust gives you size specific options for integers and floating points. So that's what, more than 20 different number types, four type scripts, one number type. But what you can control how much memory is reserved with each type instead of the inferred I30 two, which reserved space for numbers from about -2 million to positive 2 million as the IE means sign. In other words, it can be positive or negative. I can type this number as a U. Eight which reserves space for a number from only zero to positive to 55 as the U means unsigned. That's quite a memory improvement. There are lots of other types like structs, which are custom data types that look and act very similar to JavaScript objects. Enemies which are lists, a fixed named values and more. Again, the specific syntax here isn't super important at this stage in your journey. Just know that you have to give everything a type, either explicitly or implicitly, and that you have lots of options. This also means there's no any in rust. Sorry, I know. And there's not even a null type. That's because rust forces you to handle all possible errors at compile time. And that's our final topic. Error handling No language removes the possibility of errors, but rust forces you to handle them. Rust divides all errors into two categories unrecoverable and recoverable unrecoverable errors are errors that should stop your program, which rust will do with a panic macro. Your program will stop immediately and produce a trace to the here for recoverable errors. Rust provides lots of tools to force you to handle any possible issues. Let's talk about just two matching and the result type. We'll call the divide function and pass into integers. Note the match keyword matches like a switch statement, but it forces you to exhaustively handle all possible cases. When the divide function runs will get back a result type, which has two possibilities. Okay. Or error and this example will log either option. The divide function then returns an error if trying to divide by zero and the result for any other value. You may notice that you don't have to say return the final expressions and both the F and else blocks return automatically. Understanding these five conceptual differences will prime the path for your learning rust. Now I promise you for learning steps. Unlike the jungle of YouTube videos, random MDMA docs and coding tutorials with 20 ways to do everything a JavaScript and this is going to blow your mind. Rust gives you a defined learning path. Start by going to rust lang dawgs learn. Here you'll see three steps. The book is a comprehensive getting started guide for the Language. There's even a new interactive, self-guided version of the book with quizzes and fun projects along the way. Second, there's an official course that focuses on syntax and runs directly in your terminal. You can even run it in your browser. Solve each syntax error to move to the next level. Third, rust by example is a collection of examples that show how rust works in practice. Each example is heavily documented to walk you through the concepts using actual code examples. Before I mention one final step for learning Rust did you know that there's official editor tooling with helpful tips, display errors and more you'll write into your favorite editor. Rust also includes a built in formatter that acts like prettier a letter that works like s lint. And since rust is static and strongly typed, every crate has full documentation at docs dot. Rust feels safe to write because it has clear guidance and strong opinions and how you should learn, write and run your rust code. The fourth and final tool is the standard library documentation. It's famous for its clear description of all built in rust applets. If you ever get stuck, this example rich guide will have the answers and teach you rust along the way. So there you have it A brief survey of the concepts you'll need to understand as a JavaScript developer. Learning rust along with a clear learning path. Once you learn rust, spin up a full stack website with laptops or rocket build a command line tool with clop, make a game with amethyst or anything else. And if you're interested in more tutorials on Rust, let us know below. Thanks for watching.
Info
Channel: warpdotdev
Views: 231,137
Rating: undefined out of 5
Keywords: rust in 2024, rust, rust lang, rust language, programming, programming in rust, programming in 2024, rust language tutorial, rust tutorial, how to code in rust 2024, rust tutorial 2024, how to learn rust as a javascript developer, javascript to rust, rust 2024, learn rust in 2024, rust guide 2024, software development, programming tutorial, software developer, coding in rust, code rust in 2024, how to learn rust in 2024, software, technology, tech, coding tech, rustlang 2024
Id: lpSlbeJyr6M
Channel Id: undefined
Length: 10min 35sec (635 seconds)
Published: Thu Nov 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.