Transcript from the "todo, unreachable, & unwrap" Lesson
>> Unwraps, todos, and unreachables. Here's things that just don't exist in TypeScript. And so todo, this one's pretty cool. Let me show you something. So I'm gonna go in here. I'm gonna make a dir called rust-typeScript. Wait a second, what just happened here? It already exists. Well, then here, I'm gonna rm -rf.
[00:00:16] One time I deleted my home directory on Stream, in front of lots of people. mkdir, there we go. It was very bad, rust-typeScript. So brand new project right here. Don't worry, we'll go over these steps here in just one second. Cargo init, awesome. Open this up, go here, beautiful.
[00:00:33] And now I have this little main function. There we go, and now let's go in here. And I'm gonna show you a little something called a to-do. So let's create a function, and I'm going to go like this, a non-complete function. And let's just take in say, x, which is a usize.
[00:00:50] So an unsized integer, the size of my system architecture. And I wanna return a boolean. So it's gonna tell me right away, hey, you got this error. Look at that, you need to return something. If I go over here and pop up the message, expected bool, found what is referred to as the unit.
[00:01:08] Or in other words, nothing. You're not returning anything right now. So if I go like this, if x is, how about, I have co-pilot on, I've been trying it for six months here. There we go. If x is less than 10, then return true. But man, there's more code I have to do.
[00:01:25] I'm not done with the function yet. It's still erroring on me, but I just kind of want to test this. I can throw in a little todo and say, hey, me, finish this later. There we go. And so now any code below that, if you run this, will just explode and stop the program.
[00:01:42] But you have like a todo, an active todo, something you can fix later on. You may not want to complete the function yet. This is just one of those convenient things about Rust that they have throughout it, where you can kind of iterate. And so often I find that when I'm building, say, a server, and there's a specific error case that's hard to handle.
[00:01:59] I'll just throw it todo there, so that when I'm doing my testing, eventually my server blows up. And I'm, okay, yeah, I should probably start handling this now. And now's the time to start doing this. And so it makes it kinda nice to do basic dev. So there you go.
[00:02:13] There's also unreachable, meaning say we have a condition which is like only evens. You should only ever pass me an even. Well, if x modulo 2 equals 1, this is not an even, right? So I could do something like unreachable. And this just says, this is like a runtime assert.
[00:02:33] I say this should never happen, right? You can give your own little message here. And now if this happens, your program will crash with an unreachable, saying hey, you've somehow violated what you've set up as allowable in your program. You should probably fix this. This can also be very, very nice, especially when you have these weird cases where you feel like this should never happen, or you're programming something in which you know better than the compiler.
[00:02:58] And it's like anything from here on just should never, ever, happen. Boom, unreachable, and it gives you like a nice error. And then you know that somehow your belief in your own ability and logic actually is flawed. And it did, you've reached an unreachable. And we can also run this here.
[00:03:13] I can just do a little quick cargo run,and you'll see that it says nothing. I only have dead code. And so if I go only evens and pass in three, you want to guess what's gonna happen here? Well, of course, it says, hey, panicked, entered unreachable code. Just let me know, we did something terribly wrong.
[00:03:32] And then there's also unwrap. Unwrap is something pretty unique, because it doesn't really have any sort of relation to TypeScript that you can even really bring about. Effectively, what an unwrap is, is it allows you to grab out the inner value of an option or a result. Now again, those are words you're probably not very familiar with.
[00:03:51] Options, null, and undefined, result is an error. So you can imagine, I'll just create one right now. I'll show you kinda how it looks. I can go, let foo = Some(5). So if you look at its type, it's an option of i32. This thing could be undefined, we don't know.
[00:04:06] So if I were to go foo.unwrap, let bar equals this, we could technically shadow it if we want it to. Bar is just the i32, it's the thing inside the option. Unwraps are dangerous because what happens when this thing is None? Well, you can still do unwrap ,obviously needs type annotations, because nothing has told me the type.
[00:04:28] Now it knows the type because None doesn't have a type associated with it. So this is an option of i32. And technically, we now know it's None. And bar is an i32, cuz we called unwrap on it. But since we didn't realize it was a None, when we run the program it says hey, you just unwrapped a None value.
[00:04:44] You can't do that, right? That is an error, there's nothing there. You just did undefined dot something, it's the equivalent of that. And so that's kind of, it will handle options really fun. It's just know that unwraps are there, they lift values out, and they'll explode your program if you're wrong.
[00:05:00] So don't actually use them, but they're great when you're first learning. There you go, you can do the same thing with error. So if I defined an error, and then tried to unwrap an error, boom. Boom goes the dynamite, your program's dead.