RUST Enums ARE Better

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in typescript enums are considered harmful by the wizard himself Matt pocock Paco picake in typescript 5 enums have been upgraded but they are still terrible but in Rust it's a completely different story rust enums are amazing if you've never seen how they work it will truly expand the way you think programming can be done so let me show you how rust enums work by using typescript as a guide this is an enum in typescript in typescript you can create functions to take in enums and in this case when the colors red we print red when it's green green and when it's blue blue an obvious problem is if we add a new value to our enum this switch statement will give you no indication that you are forgetting a case and this is one of many issues that are brought up against enums now let's look at enums in Rust first they can be defined the same way they are in typescript this looks pretty much identical working with enums however are a bit different here is an equivalent in Behavior print color function in Rust let's look at this statement it's called a match statement it's a little bit different than a switch statement instead of comparing a value it does pattern matching now if color was red we would print out red and the same thing happens for blue and green but what happens if we add yellow this match statement will actually throw an error and you're unable to build your program because you are missing case yellow there we go we've added case yellow and our programs compiling again right now you're probably thinking all right well I guess rust has the same enums as typescript they look the same they have about the same developer experience just rust is slightly better this does not compel me to want to use rust trust me it gets much better so let's just look at the rust example for a moment something that typescript cannot do is that rust can attach functions to enums effectively they become methods so we declare hey this is an implementation for color the function's called Green part we take in a reference to the Edom and we return out a Boolean if the colors either yellow or blue which is what makes up green in the subtractive system we return true else for all of the cases we return false let's add one more method that's called is green but this time we're using a different technique so you're probably familiar with how this function is defined but it's this that's kind kind of weird right what this does is that it takes a reference for self and pattern matches it but on an if statement so only enums that are of type color green will return true the rest will return false these are both examples of pattern matching it's one way to make working with enums significantly easier methods on edoms make it so much more useful to work with but that is only part of the reason why enums are so good in Rust let me show you one more example and this is gonna be amazing because it solves one of my biggest problems I have with typescript all right let's go back to typescript and see that we've defined a custom type with name and age also a union type item that has string number or custom here would be the equivalent in Rust first we create a custom object with name and age then we have an enum item Foose the same as the string bar is the same as the number and baz is the same as the custom object let's create an item called Foo and it is a string we can tell that it's string by doing type of Foo and we can log it out let's only look at rust for a second look at how we create create Foo first we create it as an enum Foo and then we pass in a string we can pattern match out that string Again by using Foo and pattern matching item Foo with a subtype s now this s is the value of the subtype that was passed in and in our case that is a string and now we can print out hello easily we could do the same operation with bar and even the same operation with baz and the custom struct here's an example using baz here we're pattern matching baz and we get access to custom and here we can print it out let's create equivalent methods in both typescript and rust both in typescript and rust we take in a list of items and we add to that list an item with the number subtype in the case of typescript it's just 69 but in the case of rust it's item bar 69. remember these two are equivalent in values let's create a list of strings in both languages next let's call a pen in both languages in typescript we can pass in a list of strings to anything that takes a list of items that is because a list of strings in some sense is equivalent to a list of items but a list of items is not equivalent to a list of strings you can imagine that every square is a rectangle but not every rectangle is a square therefore a list of strings now will contain a number when you call a pen now for rust this won't work and the reason being is this is a list of strings this requires a list of items they are different rust fundamentally sees them as different types that's pretty cool right like not only does it help prevent any weird kind of bugs when it comes to Union types it also allows you to have these heterogeneous lists where you could have multiple types in there but you get to Define exactly which types you want that has to blow your mind enums are so useful they're even used in replacing null and undefined and how error handling Works in Rust here is the option and resolve type of course the option represents undefined slash null or some value a result represents a value or an error they use generic so they can be whatever type you need them to be something super cool that rust does that typescript doesn't when you call a function you know what it returns whereas with typescript does this thing throw an error do you need a try catch you better handle undefined of course typescript has made this a lot better but nonetheless you can still run into these situations whereas with rust it comes back as an option if it's undefined it comes back as a result if there could be an error and you can even have things like result with a value of option and this is one thing I really appreciate about rust which is when you're working with rust every function you're calling you know the exact return type and how it behaves so if this function were to air the function above would know that this function could err and would have to handle it or pass it to the next function whereas in typescript does this function even know that you could throw an error who handles the error does anybody handle the air hopefully you see why enums are amazing in Rust and hopefully you appreciated this little presentation that we went on if you like this hey please press the like button already subscribe to the channel the name is the primogen
Info
Channel: ThePrimeagen
Views: 101,706
Rating: undefined out of 5
Keywords: software, vim, programming, javascript, typescript, software engineering, web developing, web developer, software developer, developer, cpp, programmer humor, humor, reactjs, js, ecmascript, tc39, Netflix, Engineering, Engineer, Facebook, Amazon, Interviews, Software Interviews, vimrc, neovim, spacevim, vim c++, vim editor, text editor, vscode, vscode vim, vim plugins, autocomplete, vim autocomplete, nodejs, twitch, developer productivity, spacemacs, algorithms, datastructures, Data Structures, python, bash
Id: Epwlk4B90vk
Channel Id: undefined
Length: 5min 49sec (349 seconds)
Published: Mon Feb 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.