Check out a free preview of the full The Rust Programming Language course

The "Type Parameters" Lesson is part of the full, The Rust Programming Language course featured in this preview video. Here's what you'd learn in this lesson:

Richard discusses type parameters in Rust, option, and result. Option can be used to avoid errors if the value being altered the function could be nothing or null, while result is used for an operation that might fail.

Preview
Close

Transcript from the "Type Parameters" Lesson

[00:00:00]
>> And let's talk about type parameters. So here's an example of a method on a string. So this is the pop method. And what pop does is it says take one character off the end of the string and just return it. So I'm gonna say last char = mystring.pop.

[00:00:16]
Remember we talked about chars earlier, that's that type. That's basically the same thing as a u32 but it's validated to be a valid unicode scalar value. So just like, hey, just pop the public character off of my string. Now in some languages, this might be the type of last char, as it might have actually the type char.

[00:00:34]
In other words, when I call pop on my string, it's gonna give me back a char. Well, here's the problem. What if it's an empty string? What if my string is actually has no characters in it? I'm calling pop on it, then what would this be? Well, in a lot of languages that might come back as null or nil or undefined or something like that, but Rust doesn't have those things.

[00:00:52]
So what does it do? Well, one option is it could panic, but as it happens, it doesn't panic. It does something a little bit more useful than that. So here's what it actually does. Pop returns an option, bracket char. So what does that, let's talk about that. So option is an enum and its definition looks like this.

[00:01:12]
It's enum option bracket T, and then it's two variants or none, and then some parentheses T. This being the tuple syntax for having a payload. So either it's got none, or it's got some and then this T type that's wrapped up inside as a payload to the some variant.

[00:01:31]
So what is T? Well, T could be anything. It's what's called a type parameter. I could have actually chosen any name I wanted for this. I happen to choose T but I could have chosen B or X or food or hamburger, anything you want. The point is when you define it inside these angle brackets, whatever name you choose there, you're now free to use inside of your variants in your enum definition.

[00:01:56]
And as you may have noticed, there's more angle brackets here when I'm actually making one of these option things. So because I put angle brackets here the definition, whenever you're actually using this thing, you need to specify exactly what T is. So in this particular example, T is char.

[00:02:12]
But as we'll C, T can be any type you want. So for example, here's using T being string rather than char. So I could say let email: Option<string> = Some (email_str) which has the type string. Now here, this is an example of my creating an option from scratch.

[00:02:32]
Now you may be wondering why didn't I put option colon colon some here. And the answer there is that option is actually special. This is built into the standard library and you don't actually have to namespace it basically because it comes up really often. There's a lot of functions that involve option types.

[00:02:47]
And Rust didn't want you to have to write out option colon colon all the time. So basically, just made it so you can just say some or none and not have to do any name spacing for those. So the other thing we could do is we could set it equal to none either of those is totally fine, okay.

[00:03:09]
So a related type is result. So result is similar to option except that instead of representing the possible absence of a value, result represents the possible an operation that could have failed. So here we have an okay variant, in which case, it wraps up the okay. That's why the O type parameter, or we have an error variant, in which case it has a payload of E type parameter.

[00:03:38]
So there's two type parameters here rather than one. You can have as many type parameters as you want in a type. So here's an example of using that. I might say let success is result of i64 string equals okay 42, or failure of i64 string it was error and then some error string.

[00:03:53]
So this would be basically I would use this in a situation where I'm gonna run some sort of operation and either it will succeed in which case if it succeeded, I'll get back in i64. Or that operation might fail and if it failed, it's gonna give me a string describing what went wrong.

[00:04:09]
Keep in mind that this error type could be anything you want. Maybe in fact, a pretty common thing that people will do is they'll actually have an enum that enumerates all the different things that could go wrong. And then that will be what comes back as your error type.

[00:04:22]
If you don't have an error type, you could just put unit there. Or if you want, that is to say if there's no additional error information that you wanna have there, you could put unit for this error type. Or it's quite common instead to use option rather than results.

[00:04:36]
Either one's totally fine. Much like with option, the result colon colon prefix is also optional for result because again result comes up all the time. Results and option both are very common use not just in the Rust standard library but also in the whole Rust ecosystem. They're the only two enums that work this way are result and option but everywhere else you do need to actually put that prefix on there.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now