Transcript from the "Methods" Lesson
>> Okay, let's talk about methods. So we saw these earlier with the inner method. And then now we're going to talk about how to actually make your own. So let's say we've got our color enum here, and I want to introduce a method to it. Here's how I can do that.
[00:00:11] First I use the impl keyword which is short for implementation, and I'm going to say impl color. Inside that I'm going to define a function. So here I've decided to name my function rgb, rgb is going to take a color and return a tuple of three u8s. And then I'm also going to make another function called new which is going to take a red green and a blue and then return a color.
[00:00:31] And you can kind of guess what the implementations of these might be. The rgb one is gonna do a match on our color and say if it's red, then return you know, the rgb is associated with red. If it's yellow to it for yellow, if it's green to green, if it's custom, probably just returning the red, green or blue that it's gotten the custom value.
[00:00:46] And likewise with new, it's probably going to look at the red, green and blue. And decide either to turn it into a red variant, a yellow variant, a green variant or a custom as a fallback if it can't find any, any good matches. Okay, so let's say we want to define a new color, and we say let red = Color : : new 250, 0, 0, and let purple is color new 100, 0, 50, and let r, g, b = Color : : rgb purple.
[00:01:13] So what's happening here, is that we've essentially used that same names pacing concept that we did with the variants, except now it's applied to the actual color enum itself. So this is in terms of like what the functions are doing. This is not changing anything. This is just the plain old function.
[00:01:29] It's just got a namespace now. It's inside this color namespace, which means I call it by saying color colon colon in front of it. So similarly to how with variants, this is a way to have multiple variants in the same scope that are named red. Now I can have multiple functions in the same scope that are names new, I can just namespace them.
[00:01:46] According to maybe this is color : : new, but maybe I'd have something else, it's either an enum. You can also do this with structs that would say, you know, user colon colon new. You can imagine the name new kind of tends to come up a lot when you're building data structures.
[00:01:59] And similarly with the rgb function here. So, this is a way that you can use impl to namespace things. Now, there's an additional thing that you can do with this namespacing, which is what we get into methods. So let's once again say we have impl color, and this time we're going to say function rgb parentheses self.
[00:02:18] So previously we were saying color colon color, but here we're just saying self. And in an unusual step, we're actually not defining a type for this, which is the first time we've ever seen that. And here we have new, again the same as before, except instead of returning a color, it's returning self with a capital S.
[00:02:34] So what's going on here? Basically, this code will work exactly the same way as before, because self with the lowercase s and self with the capital S are special values in rust and they're only used inside of an impulse. And what they mean as you may have guessed is self refers to basically give me one of these, one of these particular types whatever your your impl is on.
[00:02:55] And self with a capital S refers to that type, whatever the impl is on. So self is basically saying you can imagine it saying self colon color, because it's saying it's automatically going to be whatever that type is. And this thing is just the same thing as color.
[00:03:10] Now the cool thing that this lets you do is this right here that we've seen at the end here. Basically, rather than calling Color::rgb(purple), now I can say purple.rgb. And all this is doing is just calling the same function passing the same argument. It's just a different way of expressing it.
[00:03:29] So this is what's known as method syntax. This is where we are calling the purple method inside the color impl. Sorry, we're calling the rgb method inside the purples impl. So purple is a color, which means that it has this rgb method. And basically a method is anything to take self as an argument.
[00:03:49] So similarly to Python you might be familiar with. This is not really doing anything more complicated than syntax sugar. Both of these two do exactly the same thing. They have the same performance. Then they compile about the same amount of speed, so it's just a matter of style.
[00:04:05] Do I want to call it in this style or do we want to call it this way? Which is both slightly more concise and also more amenable to chaining. So I can say purple.rgb dot something else, dot something else. If you know, this returns something that also has a method, which also returns something else that has a method and so on and so forth.
[00:04:21] So this is mostly just a syntactic convenience. The way we're using it here, there's nothing really anything particularly fancier going on there. Somebody asked about traits earlier self can do a little bit fancier stuff when you get to traits, but we're not gonna cover those in this introductory course.
[00:04:37] But because it's generally speaking strictly more useful than writing out like color colon color. Typically speaking, whenever you're inside an input block like this. You'll tend to see self and capital S self used in positions like this rather than using the actual color type directly.