Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course
The "Language Fundamentals" Lesson is part of the full, Polyglot Programming: TypeScript, Go, & Rust course featured in this preview video. Here's what you'd learn in this lesson:
ThePrimeagen live codes the same function in TypeScript, C++, and Rust to demonstrate some base language similarities and differences. The Rust borrow checker is tasked with enforcing that all variables are initialized before they are used, the same value can't move twice, and a value can't move while it is borrowed.
Transcript from the "Language Fundamentals" Lesson
[00:00:00]
>> So yeah, we're gonna be moving pretty fast. So can I mean if you know Vim you can also move fast. [SOUND] Okay, anyways, so I'm also not gonna try to go over a lot of syntax for. So for those that don't know rust if you know rust, don't say anything but what do you think happens right here?
[00:00:16]
>> You return either a zero or one.
>> Yes, it's assigning it to food correct. And so if you've programmed before you can kinda mostly deduce what is happening. So I'm going to rely on your skills to be able to do that like I'm not gonna explain what an if statement is.
[00:00:32]
I'm not gonna explain any syntax for the most part. I will explain a small part about Rust that I think are the more confusing parts or the different parts from other C based languages. And same with go I'll kinda go okay these are pointers these aren't pointers. We're not gonna go too deep into them because it's just not that valuable knowing how to use it versus what is actually going on you don't necessarily need to know these as much in these languages versus this is not C where it's it feels dramatically more important to know these things.
[00:01:02]
And so, exact same thing TypeScript version, right? Boolean statement ternary operator. I couldn't use the Add on or OR or but I still feel disgusted by that but even to this day. All right and I think actually did I put the logic backwards? No I didn't. Okay good.
[00:01:16]
It would actually be very hard to do the and on or or here. So the first thing that we're gonna kinda talk a little bit about is the Borrow Checker. Now this is obviously the most kinda famous thing about rust that makes it super duper difficult. And we're just gonna go over this a little bit.
[00:01:30]
Just enough for you to understand it. And there's some few simple rules that as long as you keep those rules in your head. Rust is just it almost feels really simple looking in the rearview mirror but when I was first learning it it felt really obtuse and hard.
[00:01:45]
And so just like all things, simplicity comes after you know it, and then you look back, and you're like, how do people did not get this, right? It just feels so simple once you look back. So don't feel bad in it. So I'm gonna jump over here, and I'm just actually gonna program a little something, just so that we can all feel it's all here, we'll go like this, we'll call it playground?
[00:02:05]
That seems about right, playground project, yeah, let's go in here. All right. Before we do that, always remember whenever you're doing anything's a net why gotta get these things going cargo and net. We'll go over all this a little bit more in a moment. I'm just kinda creating some things to show you a little bit how it feels, cargo init and go mod init github.com/theprimeagen/doesn't matter.
[00:02:34]
There we go. All right. Perfect, we have all three up and running. Everything looks good. And I'm gonna go yarn add ts-node typescript @types/node. Gosh. All right, now I can officially program and TypeScript, I'm ready. All right, so let's just do a little quick like little just we're just gonna look at how this looks.
[00:02:56]
So if I were to write a little function or if I were just to execute some code I said const a equals an array and then I said const b equals a. And then I did b d.push.1 and I logged out console.log a. What's gonna happen here? What am I logging out?
[00:03:15]
What's gonna log out with a?
>> One.
>> Yeah an array of one, right?
>> Yeah.
>> An array of one. And why is that? Because b points to a underneath the hood, these are actually they're technically containers underneath, like va has these little boxed values, that point to stuff, and that's how they manage it.
[00:03:31]
And there's a bunch of management that goes on and blah, blah, blah. You don't really care about it. But what you care about is that a refers to a value. That's an array. B refers to a, and can manipulate what a's value is, right? And so that's what happens here.
[00:03:47]
So when I log out a I should see whatever b did to manipulate it. And now we do this implicitly all the time we don't really think about it but underneath the hood if you know if you come from a c world this just pointers, right? You're just kinda handing out pointers and people can manipulate it and that's what it is.
[00:04:02]
That's why JavaScript is both so fun, and also so annoying. Because even though I've declared this thing, as you cannot change, you can change this, not changeable because sometimes not changing is unreasonable. And so ,that's just how JavaScript decided to go. So let's do that again, but let's do it with CPP.
[00:04:20]
Everyone's favorite language of all time, right? And apparently I can't spa a language server, don't worry about that. We don't need that language server anyway. So we're gonna go into main and we're gonna jump in here and I'm gonna go include vector and stdio. Can I just do this I always forget we'll find out when we compile.
[00:04:42]
So there we go we have our includes and now I'm gonna go like this vector we'll put in and we'll go a will do the exact same thing and go b. And b will equal a. And I'm gonna go b push_back 1. And now of course because this is beautiful C++ you can't just pass that and print a vector.
[00:05:04]
What is that you can't do that? So we're gonna just do this nice little const auto our cansado we won't do that, std vector, a int and a reference to it. And we'll just call it a and for consnt auto item in a and we'll printf, Item sorry, just give it a second printf vector.
[00:05:35]
There we go. And of course print f and a new line. Look at how beautiful that is. See, this language is simple. All those people saying that C++ isn't simple, they just don't understand it. All right, print f a, and of course print a. So what do you think is gonna happen here?
[00:05:53]
Probably the same stuff, right? So let's go over here. And go, I never actually tested it. Okay, good. I just realized that I might not know up. Yep. Okay, go in here. Perfect. What is it? ./a.out. Yeah. So a vector, it has nothing in it. Well, let's try would b, that seems a little weird because we just said b equals a and then we did that.
[00:06:20]
So for fun, let's put b in here. There we go, and recompile and re execute. So look at what happened here. Is that a little strange? For a lot of people that have only ever worked in the JavaScript world, this may seem a touch strange. Well, what's actually happening is that when you do this, the vector is copied from a into b.
[00:06:43]
There are two separate vectors which means that I could come here and create a vector c, put that in here, go here and do another one. Take this, take b, turn into c, and execute it again. And now, as you can see, that's a little bit different, right?
[00:07:03]
You may or may not expected that to happen. So this is a different way you can think about programing. Obviously, C++ gives you the ability to do different operations. There's move constructors, there's copy constructors, they have a bunch of ampersand signs in them. I don't remember exactly even how to do them because I rarely do them but there's a lot to C++.
[00:07:22]
There's a lot of ways to shoot yourself in the foot and this is one of the many ways that I have personally shot myself in the foot and it's fantastic. But the reason why I bring this up is that we often don't think about variables if you've come from the TypeScript world like this, where there's actually different behaviors based on the equal sign.
[00:07:41]
And so Rust has its own behavior based on the equal sign. So let's kinda check that out. Yep, we're not the I did a shorter example here. I gave you guys the premium version of this example. All right, so while you come here, so let's do this again.
[00:07:57]
Except for this time, let's do it in Rust. Now I'm gonna get this nice long air for a second and then it's gonna go away. And I'm gonna go, let a equals vec bang this, all right? And then I'm gonna go b equals a. Here I'll even define a, we don't even need to define a.
[00:08:15]
And go. We'll call it mute, b.push, and we'll put in the number one. There we go. Now a has been defined, b has been defined. It's a vector of i 32. For those that don't know vector, you can just think of it as like a list, a linked list, an array list, whatever it is underneath the hood.
[00:08:34]
You don't have to know what it is. It's just a list of some sort, all right? And so now of course we can go print ln in the sense if this language is better we can do this beautiful greater operator and we can print this out, right? This makes.
[00:08:49]
We probably all can guess what's about to happen. So I'm gonna go cargo run we see an array of one. Awesome. I'm gonna also make obviously a mutable and it says hey you don't have to do that well let's go a.push1. What happened here? It's saying you can't do that, right?
[00:09:08]
Why is that? Well, there's a very specific reason in rust and once you get this mental model in, Rust become significantly easier to use. It just takes a little bit of time.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops