Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course
The "Enums & Rust Q&A" 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 answers student questions regarding whether you can have an enum that points to itself, why the map has a reference, and what the enum's owner is. A brief discussion regarding macros in Rust is also covered in this segment.
Transcript from the "Enums & Rust Q&A" Lesson
[00:00:00]
>> Can you exception an email so an email them pointing to itself
>> No because there is, I think you can, I want to say you can't do that but the reality is is that I don't believe you can do that so let's just try it out and so I'm gonna go bar with a foo.
[00:00:18]
Yeah you cannot pick as it has this whole problem where how big is the underlying type well it has to be as big as its biggest member. So if it has anyone here worked with C unions? C unions effectively, if you have something that's eight bits long, and something that's 64 bits long, it always has to take up 64 bits in memory, because it does not know how big it's going to be.
[00:00:40]
Is it gonna be eight or is it gonna be 64? Well, this thing has to do the same thing, so if you have an enum that has a type bar that has a subtype foo, well, what can foo contain? Well, foo can be a bar that contains itself that contains another version of itself or what can that be?
[00:00:55]
So how big is that thing, well it's obviously infinitely big so cannot create this. That's what I thought was gonna happen but I just wanted to make sure just in case I was wrong. I've been wrong before and I know [LAUGH] It happens. All right any other questions?
[00:01:14]
This feels a bit confusing, right? This feels like a little bit overwhelming I assume it does, that's because you're not doing anything, right? Or any of you programming doing stuff, no, it's not setting in right it's not setting into your heart I just wanted to cover these things so you see them.
[00:01:27]
So when we actually do them, it becomes a lot easier to go okay, yeah, there's options. Okay, I remember at least remember hearing these words, let's see it.
>> So I can think of match as kind of a switch statement.
>> Match is a switch statement that's just a little bit more aggressive, right?
[00:01:43]
A switch statement usually, which in traditionally a switch statement only contains constants, because it's some sort of offset. That's why like in the JVM, if you run a switch statement, it consumes a finite amount of bytecode. Like if you actually measure how many byte codes are executed no matter how many match statements you have, it only executes the same amount of bytecode.
[00:02:03]
And that's because it's not doing any processing, it's just doing like a jump, it's like the same cost as an F cuz it's kind of pre computed in. I think with JavaScript you can be a little bit more loosey goosey, I think you can have variables in there.
[00:02:12]
I can't remember I don't think, I want to say you can't but I don't know JavaScript, crazy. Whereas match statements, they only, they work on types, so you're kind of typing off what's inside there. And so that's kind of different, so that means you still get that really fast jumping cuz every enum is stored with what is called the discriminator.
[00:02:31]
So it knows its type in memory, so when it does this nice match statement, it doesn't have to do this really complex breaking down, very expensive operation. It's just like, I'm third offset in, this thing has a subtype this, okay, I'm fourth offset in, right? So it can go pretty fast.
[00:02:47]
>> [INAUDIBLE] I noticed the map you didn't have to reference, right [INAUDIBLE]
>> Yes.
>> But for the filter you do have to reference.
>> Exactly, so think about this, I wish JavaScript was a little bit better with this, but if you think about it when you are mapping, you are taking a value and you are converting into a new value.
[00:03:09]
Now with Rust what happens if I hand you the value, do I own it anymore? No, you have the value but your responsibility in the map is to give me a new value at that point, so that's how Rust does it. Whereas a filter, I'm saying, do you even wanna keep this value?
[00:03:23]
That means I must retain it but you tell me a yes or no and so that's why filter has a reference and map will not have a reference. So if you're doing say, there's there's ways to do iterators or we'll just do something really quickly in let's see I'll mark this do you want to.
[00:03:40]
Those there we go, so if I do this, you'll notice that the type is a char, right? Because I'm creating an iterator but if I do this, the type it's going to say you can't do that. But it's going to be a we'll deal with another one but you can do things in which I do this.
[00:03:58]
No, that'd be sweet thing, either way you'll be able to do maps over references versus maps consuming the value. And so there's this concept called iterator or dot iterator, so on a vector. If I go Let a = a veck, this is probably a better example anyways, I can call it or on it, and this creates an iterator, but I can also call into iterator.
[00:04:19]
Now this returns to different values in Iter Hanzi references to itself where as a into iterator converts your vector into an iterator and now you cannot reference that vector anymore because you've handed off the value and it's handed you back an iterator. And so it converts the values, so if you do an iter, and then you do a filter, obviously, that is a find, a filter, and let's just go x.
[00:04:46]
Now let's just give this a quick type, i32, you'll notice that since I did an iterator, remember, this is a reference to your items. That means it's going to be a reference to your referenced items is my guess and as you can see it's a reference, reference, right?
[00:05:04]
That makes sense once you kind of understand the outer concept which is just, when you do an iterator, it's referencing all of your items. If you do a filter it always references whatever items are being done via the iterator. Because the filter doesn't have the context to know, it's fine for me to hand you back this reference because I know it's right, it just it just operates on, you know, types.
[00:05:25]
So, of course, then there's more to it but all right,
>> So mute of value is the owner, correct?
>> Well, the owner is the thing that's not a reference, so a owns the vector right here, right? A owns that right there, A owns the vector, I can hand out references to that vector I can hand out mutable references to that vector if A is mutable itself, but I cannot have two owners of that value.
[00:05:54]
Hence the reason why when we did this, the value of a gets moved to be you can no longer refer to a anymore within your program. That seems very trivial and almost annoying but it's actually quite fantastic as time goes on and the funny part is is that I do this contest all the time on my stream which is first try.
[00:06:13]
Can I build something first try? And with TypeScript, I'm about a 20% success rate, with Rust I'm a 75% success rate despite writing more code and I'd argue it's definitely more complex. I just feel I get it right more often, like the pit of success is just easier to fall into despite it being harder.
[00:06:33]
And how I kind of like to think about it my head is in people because you get this all the time. Well, developer ergonomics type scripts so much faster my argument is okay, let's say you had the run a lap on the track, Typescript runs at 10 miles an hour.
[00:06:46]
Rust runs at seven and a half miles an hour, well if, you get it wrong you're running another lap with TypeScript, right? So yeah, I may not finish as fast but I often don't have to run that second lap, .that call costs a lot more ultimately in the end.
[00:07:00]
And that's kind of one of the reasons why I've been enjoying using this language for tooling for various things. I've even played around with it on one with wasum, it's super outrageously radical with wasum so Rust has this thing called macros, I just want to find the syntax.
[00:07:16]
Macros are usually considered the devil and I totally am on board with it being the devil but starter templates examples, there we go. Edit this page, awesome. That's exactly what you wanted to see. And so let's just do, look at that game of life, what do we want to do?
[00:07:32]
Simple SSR to do MVC, why not? That seems pretty reasonable, let's just look at does this have some HTML in it? HTML, all right, look at this, does that look like React? Yeah, pretty much you're doing very, very similar things as you would do in React. So Rust has a super powerful macro system and people make the coolest stuff.
[00:07:58]
You'll see it we'll be using a macro or two coming up and so you have things that look just like something you're already familiar with and even has hooks and everything else, right? It has a good state all that so you can get these pretty fun concepts because of how powerful rust can be.
[00:08:13]
Now I personally don't write any macros because I've never not shot myself in the foot but I do know they're useful. I do know that you can do things of them, I just always end up doing the wrong thing. It seems like-
>> Will two references to the same constant number in Rust be equal?
[00:08:27]
>> Yes, I wanna say yes, so you're saying like this alright, B equals 5, C equals A reference to B, D equals A reference to B if C equals D. First off there are types the same, right? And a reference to an I 32 and a reference to an I 32, You can always have the same types.
[00:08:44]
Now if I did this, they wouldn't be the same because what is the problem you you have, of course D is a double reference. C is a singular reference, now I can strip one of the references off of D by doing this. There you go, or I can add a reference to C by doing that and so it's just reference playing now, you don't really deal with pointers in Rust, there's just references.
[00:09:07]
I know that's like such a subtle difference but the difference with the pointer is that you can kind of move a pointer around whereas with a reference. I can't really move a reference around I either dereference the thing or reference it, right? There, eight pointers you can plus one to it and boom you've just moved the width of the item.
[00:09:24]
And now you're often magical stack smashing land which by the way I've smashed many, a stack in my day pointer arithmetic does take place but it's in unsafe mode. I believe you can do it in unsafe mode so if you get really adventurous which I've still not even used on safe mode once.
[00:09:36]
I've never needed to use it I believe you can do pointer arithmetic but then you leave the bounds of safety the strong Rust won't hold you at night telling you everything will be okay, you're off doing. Who knows what and it's dangerous-
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops