1 00:00:06,110 --> 00:00:08,420 Methods are similar to functions. 2 00:00:08,450 --> 00:00:15,200 They differ because methods are defined within the context of a struct, enum or trade object. 3 00:00:15,500 --> 00:00:21,830 Methods always have their first parameter as self, which is representing the instance of the drug that 4 00:00:21,830 --> 00:00:22,970 is calling the method. 5 00:00:24,230 --> 00:00:27,620 So let's create a struct real quick called square. 6 00:00:29,330 --> 00:00:35,750 And we're going to have the width and height. 7 00:00:39,000 --> 00:00:40,500 Then let's create 8 00:00:42,930 --> 00:00:43,710 a square. 9 00:00:44,980 --> 00:00:49,810 We're going to say the width is five and the height is five. 10 00:00:51,260 --> 00:00:51,530 All right. 11 00:00:51,530 --> 00:00:51,950 So awesome. 12 00:00:51,950 --> 00:00:53,390 So we got our square created. 13 00:00:53,690 --> 00:00:55,370 So in order to. 14 00:00:56,390 --> 00:00:57,860 Create these methods. 15 00:00:57,860 --> 00:01:05,090 We start off with the simple keyword which stands for Implement and then the name of the structure that 16 00:01:05,090 --> 00:01:06,230 we want to implement these on. 17 00:01:06,230 --> 00:01:14,990 So we want to implement some methods on a square and then just like what's normal with a function, 18 00:01:14,990 --> 00:01:21,650 we say function so f an area and then remember the first parameter is always self. 19 00:01:21,890 --> 00:01:29,390 And then in this case we want to return an unsigned 32 and since we're doing the area we're going to 20 00:01:29,390 --> 00:01:30,980 do with. 21 00:01:32,490 --> 00:01:33,210 Times high. 22 00:01:33,860 --> 00:01:43,010 So if we come down here and print this out, we would expect to see 25. 23 00:01:45,940 --> 00:01:47,410 And that's exactly what we got. 24 00:01:47,410 --> 00:01:53,400 So we can have as many of these functions as we want. 25 00:01:53,410 --> 00:01:54,160 So is another one. 26 00:01:54,160 --> 00:01:59,650 Let's just call What's my width if I can spell it right. 27 00:02:02,570 --> 00:02:05,420 And we're going to refer to self. 28 00:02:06,590 --> 00:02:17,890 We're going to return an unsigned 32 and we're just going to return self with and again, just to see 29 00:02:17,890 --> 00:02:19,540 how this works, let's print it out. 30 00:02:25,800 --> 00:02:28,050 Field, not a method. 31 00:02:28,230 --> 00:02:31,170 And that's because it's called What's My Width? 32 00:02:32,280 --> 00:02:33,240 That would help. 33 00:02:34,920 --> 00:02:36,510 And we got five back. 34 00:02:36,900 --> 00:02:40,440 So that's how you create methods with. 35 00:02:42,130 --> 00:02:42,810 Trucks. 36 00:02:42,940 --> 00:02:47,170 But now what if we want to have a mutable stroke? 37 00:02:47,260 --> 00:02:49,720 How can we go about that? 38 00:02:51,030 --> 00:02:55,830 Well in here we would need to make square mutable. 39 00:02:57,840 --> 00:03:08,310 So now that we did that, so let's create a function called change width and we're going to obviously 40 00:03:08,310 --> 00:03:10,380 exempt ourself. 41 00:03:10,380 --> 00:03:24,480 And then I also want to say, hey, new width of type use 32 and then we're going to say self dot width 42 00:03:24,900 --> 00:03:26,880 equals new width. 43 00:03:28,710 --> 00:03:41,670 So down here we would say square dot change with in my new width we're going to make it ten, which 44 00:03:41,670 --> 00:03:43,460 even though now makes it a rectangle. 45 00:03:43,470 --> 00:03:46,620 But for the sake of this, we'll say it is still a square. 46 00:03:48,270 --> 00:03:55,920 So now if we print it out again, we would expect it to print out ten now, which it does not. 47 00:03:55,920 --> 00:03:57,240 So let's see what my error is. 48 00:03:57,240 --> 00:03:59,850 So it says self is a reference. 49 00:03:59,850 --> 00:04:03,570 So the data it refers to cannot be written. 50 00:04:03,570 --> 00:04:04,950 So we need it to be. 51 00:04:06,050 --> 00:04:06,860 And. 52 00:04:07,750 --> 00:04:11,880 Mutable and now that it works. 53 00:04:11,890 --> 00:04:20,890 So we updated our width through a mutable reference to the square struck. 54 00:04:20,890 --> 00:04:27,640 So it works the same way as we learned in the last section with mutable references. 55 00:04:27,640 --> 00:04:36,040 But in this case we had to make our self mutable and the function call, and then we also had to make 56 00:04:36,040 --> 00:04:38,200 our variable mutable. 57 00:04:39,330 --> 00:04:46,830 So in the next lecture, we're going to look over what life times are, as well as the history and why 58 00:04:46,860 --> 00:04:48,630 lifetimes are. 59 00:04:50,010 --> 00:04:54,390 Even in rust and what exactly they do for us.