Check out a free preview of the full Rust for TypeScript Developers course:
The "Introduction" Lesson is part of the full, Rust for TypeScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen begins the course by comparing Rust to TypeScript. The developer ergonomics of Rust make it easier to write maintainable code without a lot of unexpected behavior. Proficiency in TypeScript and an understanding of Unions, Generics, and functional programming are recommended before viewing this course.

Get Unlimited Access Now

Transcript from the "Introduction" Lesson

[00:00:00]
>> Today, we're gonna be learning about Rust. I hope everyone's really excited about it. I've specifically tried to make this the easiest transition from TypeScript to Rust. So a lot of these examples will be using TypeScript as a foundation, so that way you can relate what you already know to something you don't know.

[00:00:17] And here's the things we're gonna be talking about. I know some of you will be disappointed by the list, because no, there's no async, no, there's no lifetimes. Those are very hard to kinda fit into such a short period of time, so we're just gonna try to really get the foundation.

[00:00:30] The words that are confusing the first time you hear them will be less confusing by the end. And by the end, you should hopefully have the skills to google your way through a mid-sized CLI application, something like that. Some assumptions I'm gonna make, I hope that you don't take this offensively.

[00:00:48] You're a bad programmer. Don't worry, I'm a bad programmer. John Carmack, good programmer, also can choke you out with Brazilian Jiu-Jitsu. I assume you can do TypeScript. Everyone in here I've already talked to, they can do TypeScript. I'm looking for someone that understands this right here. This is about the level that I would expect, is that this shouldn't be very hard for you to understand.

[00:01:09] This is just simply a promise that has been exploded, grabbed all the parts and I'm returning it. And if you can understand how this generic plays into this, we're looking pretty good then. Then you're at about the right TypeScript level. We're gonna get just slightly more complicated than that with Rust.

[00:01:27] All right, everyone feels good? Is this confusing to anybody, other than why would you do that? Actually, I do this for an interview question all the time, it's fantastic. All right, so why Rust? Well, there's a bunch of reasons, but two are very important to me. Number one, ergonomics.

[00:01:44] Everyone loves that word. It's like the Steve Ballmer chant of our generation. He wouldn't yell developers UDL, developer ergonomics over and over again now. And so this may seem kind of counterintuitive cuz everything you've heard about Rust is it's super, super hard. Well, actually, it's one of those things that if you can remember your first time programming and learning how to program, how a function works was super, super hard.

[00:02:08] How classes work, it was super, super hard, method, super, super hard. Maybe a for loop was kind of easier, you can kinda logically walk through it. But anytime you have to learn something kind of novel, it's always super hard, and Rust just has more novel things in comparison to say something like TypeScript.

[00:02:25] TypeScript is a fairly easy language, so you don't have too many hurdles to jump. Whereas Rust has more things to jump, but once you've done it, there's things that are very easy about Rust that are just extremely difficult in TypeScript to do. And so it's more expressive. It allows a different way to look at the world.

[00:02:43] And that's really good to kind of have in your head. And some really specific things is specifying read-only versus mutation. JavaScript, or TypeScript specifically, you say read-only. And read-only doesn't always work if you don't know. If you have read-only properties and you pass it to a function that will mutate those properties, TypeScript just says, hey, that's okay cuz read-only properties don't exist, whereas with Rust, you have to specify when you can mutate it.

[00:03:08] It's kind of a fundamental design decision that's a little bit different which allows you to always think ahead where you want to change something. That just means you're gonna be thinking more and more about your program, you're making less and less mistakes. A good classic example is array.reverse, it returns an array.

[00:03:24] So your initial thought is that it just returned you a new array that's reversed. Well, joke's on you, it's just returning you your own array, and it's reversed it in place, and you didn't know until you've experienced calling that function, and it's been reversed on you. Undefined and null versus option, options, at first, are kind of annoying to work with, why would you want options?

[00:03:46] Undefined, null, pretty easy, but after some time, you realize how good an option is, and how annoying undefined is. Lastly, I think this is the biggest one for me in the ergonomic department, errors as values versus errors being thrown. So JavaScript, you throw errors. But what's worse about JavaScript is that you don't even know what functions throw errors.

[00:04:07] You know that 0.1% of always failing code in production and no one knows why it's failing? Well, you can't know because no one tells you where things can break. You just have to discover it with passion and late nights, that is how you do with JavaScript. Whereas Rust, it's gonna tell you before you program, this function has a possibility of erroring, you need to think about it.

[00:04:26] But it doesn't have the whole if or equals nil problem of go, does not equal nil. And then second one, skill gap, yes, skill gap, what do I mean by this? Typescript is really, really simple. If you are already familiar with programming, say you have learned Python, you've learned some other language, you could learn pretty much enough to build anything of your same skill level in TypeScript in an afternoon.

[00:04:49] It's not a complicated language. It may feel complicated. Long as you're not doing the complex XState library stuff and you're just doing just application programming, it's a simple language. There's not a lot to it, and so your expressiveness is limited by what you know about it, and there's just not a lot to know about it.

[00:05:06] Whereas Rust, there's a lot of deep topics, things I still don't, I don't know how to do procedural macros yet. There's a lot to the language that is just immensely deep and you can do just the coolest things with it. So for me, I really like that. I like something in which I can invest more time into and learn new techniques to do things.

[00:05:22] I'm not just simply, well, this is what I use, so now I gotta go find out ways to do things. And some things in TypeScript are just hard. A good example would be untagged unions, if you don't know what that means. So a string or a number, you have to do runtime type guards to find out what it is.

[00:05:38] And that can be very frustrating if you've ever ran into that kinda stuff, especially if it's really large. So my goals today are this. I wanna make sure that at the end of this, you can just build or google your way to be able to build an application.

[00:05:50] I think that's really important, because I can't make you good at Rust in one day, it's a harder language to get initially over. But I can at least define the words so that when you see them again, you have a good idea what they mean and it won't be so confusing.

[00:06:03] Cuz the first time I heard option, the first time I used an option, the first time I used a result, or the way enums work in Rust, it was very hard or complicated or unusual for me. And so it just takes a little bit longer to kind of wiggle those things out.

[00:06:17] Here's all the things we won't cover. I'm not gonna cover creating your own errors. Those small portion where we're actually defining errors in our application, we're gonna use a library called anyhow. It just makes it super easy for application code to create errors. We won't be doing any Wasm/UI stuff, no async stuff, no smart pointers and interior mutability.

[00:06:36] I know,, $10 implementations, but for you, it's like a $1 API. Very, very simple to use them, but they are very actually complicated. No lifetimes, again lifetimes, they're very hard to get to the point to show you something where you can use them in a real sense. It's easy to show you a toy example.

[00:06:51] I could right now just show you a struct that has a reference and has a lifetime, and yay, but it won't hit home unless if we can get something that's worth it. So I couldn't come up with something that is easy enough to get to, and so we just didn't do it.

[00:07:05] Macros, both procedural macros or declarative macros, these are super awesome, but at the same time, they're just too complicated to try to fit into this time slot. So here's a cool example of one right now. Anything that has a bang in it, that's a macro. So view bang, this is real code right now.

[00:07:22] Leptos has this, a couple other libraries that work on the front-end have this, where you can write effectively JSX inside of Rust. But here's the cool part, there's no transpiler, there's no any of that, it just works, works with your LSP, everything's great. That's because in Rust, macros are a first class citizen, you use tokens and stuff.

[00:07:41] Not like the C ones, if you're used to C, C is like, I'm just gonna effectively do text replacement. Whereas this one is, I'm gonna do token expression and manipulate them and build you out actual AST. And so it's super, super cool. Not a lot of fun and I'm very, very bad at them.

[00:07:58] I didn't make that. Who am I? I am CEO, CEO, CEO, and CEO and Chairmen of the board of TheStartup. Here's some pictures of my Twitch and YouTube. I do a lot of that content, I think that's probably what I'm best known for. I also work at Netflix, but I feel like that's just not as cool as my YouTube or Twitch.

[00:08:17] And there you go.