1 00:00:06,630 --> 00:00:09,240 - So we've seen how to declare structure types. 2 00:00:09,240 --> 00:00:10,320 What we're going to look at next 3 00:00:10,320 --> 00:00:12,510 is how to create this structure instance 4 00:00:12,510 --> 00:00:14,910 and to access its fields. 5 00:00:14,910 --> 00:00:17,220 So to create a structure instance, 6 00:00:17,220 --> 00:00:20,640 you use the name of the structure like employee, 7 00:00:20,640 --> 00:00:22,830 followed by curly brackets. 8 00:00:22,830 --> 00:00:24,600 And inside the curly brackets, 9 00:00:24,600 --> 00:00:26,970 you initialize all the fields, 10 00:00:26,970 --> 00:00:30,330 the name, the salary, et cetera, in any order. 11 00:00:30,330 --> 00:00:31,740 Here's an example. 12 00:00:31,740 --> 00:00:35,670 I've created an employee instance or an object, 13 00:00:35,670 --> 00:00:37,170 and inside the curly brackets, 14 00:00:37,170 --> 00:00:39,900 I've initialized the name to be a string Jane, 15 00:00:39,900 --> 00:00:44,430 salary 1,000 and a full-time status as true. 16 00:00:44,430 --> 00:00:47,340 Okay, so an important point about structures 17 00:00:47,340 --> 00:00:49,950 is that they're allocated on the stack. 18 00:00:49,950 --> 00:00:51,690 They're not allocated on the heap. 19 00:00:51,690 --> 00:00:54,120 There's no dynamic memory being involved. 20 00:00:54,120 --> 00:00:55,740 There's no garbage collection. 21 00:00:55,740 --> 00:00:57,330 Structure instances, when you create them, 22 00:00:57,330 --> 00:00:59,670 I guess they're just allocated on the stack 23 00:00:59,670 --> 00:01:01,230 which means at the end of the function, 24 00:01:01,230 --> 00:01:03,900 they get popped out the stack and they disappear. 25 00:01:03,900 --> 00:01:06,480 Okay, so the memory model in Rust 26 00:01:06,480 --> 00:01:09,870 is quite simple really compared to other languages. 27 00:01:09,870 --> 00:01:13,680 Structures get allocated on the stack and their lifetime, 28 00:01:13,680 --> 00:01:15,270 if you create them this way, 29 00:01:15,270 --> 00:01:19,110 is bounded by the function in which they're defined. 30 00:01:19,110 --> 00:01:21,573 When they go out of scope, they get dropped. 31 00:01:22,590 --> 00:01:25,830 Right, well, so that's a structure instance. 32 00:01:25,830 --> 00:01:28,410 What if you want to have a mutable structure instance, 33 00:01:28,410 --> 00:01:30,810 one where you could change the fields? 34 00:01:30,810 --> 00:01:32,730 Well, you just use the mut keyword 35 00:01:32,730 --> 00:01:34,560 in the variable declaration. 36 00:01:34,560 --> 00:01:35,778 Here's an example. 37 00:01:35,778 --> 00:01:39,183 e2 is a mutable object, it's an employee. 38 00:01:40,530 --> 00:01:43,800 Its name is John and its initial salary is 1,000. 39 00:01:43,800 --> 00:01:46,560 And John is a not a full-time employee. 40 00:01:46,560 --> 00:01:50,190 Okay, so the employee is mutable. 41 00:01:50,190 --> 00:01:53,670 Notice that mutability applies to the whole object. 42 00:01:53,670 --> 00:01:55,920 When you declare a mutable employee, 43 00:01:55,920 --> 00:01:57,510 all of its fields are mutable, 44 00:01:57,510 --> 00:02:00,480 the name, the salary, and the full-time status. 45 00:02:00,480 --> 00:02:04,260 You can't define mutability on a field-by-field basis. 46 00:02:04,260 --> 00:02:08,493 The whole structure object is either mutable or not. 47 00:02:09,510 --> 00:02:11,970 Right, what about accessing fields in the structure? 48 00:02:11,970 --> 00:02:14,790 Well, no surprise, use a dot. 49 00:02:14,790 --> 00:02:17,940 So let's say I've created an employee object, e1, 50 00:02:17,940 --> 00:02:19,860 and I've given it some initialization. 51 00:02:19,860 --> 00:02:21,780 You can imagine, can't you? 52 00:02:21,780 --> 00:02:26,340 You say e1.name, e1.salary, e1.fulltime, 53 00:02:26,340 --> 00:02:28,620 okay, assuming that you've got access to those fields 54 00:02:28,620 --> 00:02:31,770 if they're in the same module and if they're public, 55 00:02:31,770 --> 00:02:33,393 so straightforward enough, 56 00:02:34,320 --> 00:02:37,050 if the structure variable is declared as mutable, 57 00:02:37,050 --> 00:02:39,810 then of course you could change the fields if you wanted to. 58 00:02:39,810 --> 00:02:42,840 So e2 is a mutable employee 59 00:02:42,840 --> 00:02:45,150 and I've doubled the employee salary, 60 00:02:45,150 --> 00:02:46,710 a good day in the office, 61 00:02:46,710 --> 00:02:49,920 and then I've output the employee's name, the new salary, 62 00:02:49,920 --> 00:02:51,720 and the full-time status at the end. 63 00:02:52,650 --> 00:02:55,560 So quite straightforward, no great surprises there. 64 00:02:55,560 --> 00:02:57,386 Let's have a look at the example in the project, 65 00:02:57,386 --> 00:02:59,640 lesson10_structs_getting_started, 66 00:02:59,640 --> 00:03:03,300 the files we're gonna look at, well, mytypes.rs, 67 00:03:03,300 --> 00:03:07,350 it's the same employee structure type as we saw previously. 68 00:03:07,350 --> 00:03:11,010 Main.rs, it declares that module 69 00:03:11,010 --> 00:03:14,220 and runs the code for this part of the demo, 70 00:03:14,220 --> 00:03:17,190 the code for this part of the demo, demo_instances. 71 00:03:17,190 --> 00:03:19,080 Okay, so we'll take a look at the code 72 00:03:19,080 --> 00:03:21,900 and then we'll run the project, let's see. 73 00:03:21,900 --> 00:03:25,650 Okay, so quick reminder, here's mytypes.rs, 74 00:03:25,650 --> 00:03:28,110 a public structure with public fields. 75 00:03:28,110 --> 00:03:30,780 Remember, both need to be public. 76 00:03:30,780 --> 00:03:32,370 So if you just declare the structure as public, 77 00:03:32,370 --> 00:03:35,430 but not the fields, then these fields would be private 78 00:03:35,430 --> 00:03:38,970 to this file, which isn't really what we wanted to retrieve. 79 00:03:38,970 --> 00:03:43,140 We want them to be accessible elsewhere so they're public. 80 00:03:43,140 --> 00:03:47,250 In main, I import, I declare the module, 81 00:03:47,250 --> 00:03:50,370 plus all the different code modules as well. 82 00:03:50,370 --> 00:03:52,290 Demo_instances is the module 83 00:03:52,290 --> 00:03:54,570 for this particular part of the chapter. 84 00:03:54,570 --> 00:03:59,250 So here is the call, let me uncomment demo_instances, 85 00:03:59,250 --> 00:04:00,153 and it's here. 86 00:04:01,200 --> 00:04:04,500 So I've got an example that creates immutable instance 87 00:04:04,500 --> 00:04:06,150 and I've got a demo exception 88 00:04:06,150 --> 00:04:09,660 that creates a mutable instance. 89 00:04:09,660 --> 00:04:12,360 So here is an immutable employee 90 00:04:12,360 --> 00:04:16,530 whose name and salary and full-time status are fixed 91 00:04:16,530 --> 00:04:18,783 and here is a mutable employee. 92 00:04:20,070 --> 00:04:21,720 So the whole of the object is mutable. 93 00:04:21,720 --> 00:04:23,100 I choose to change the salary, 94 00:04:23,100 --> 00:04:25,650 I could have changed any field that I like 95 00:04:25,650 --> 00:04:28,350 and then I'll output the details there. 96 00:04:28,350 --> 00:04:29,910 Right, again, that's quite straightforward, I think, 97 00:04:29,910 --> 00:04:31,200 really let's just give that a run 98 00:04:31,200 --> 00:04:33,453 just to make sure that it all works fine. 99 00:04:34,470 --> 00:04:35,583 Cargo run. 100 00:04:38,430 --> 00:04:40,560 Okay, so Jane was the initial employee 101 00:04:40,560 --> 00:04:45,270 with a fixed salary of 1,000 and full-time was true. 102 00:04:45,270 --> 00:04:47,550 And then John, which was mutable, 103 00:04:47,550 --> 00:04:51,420 had a double salary and his full-time status false. 104 00:04:51,420 --> 00:04:53,343 Okay, so that's quite straightforward. 105 00:04:54,270 --> 00:04:56,040 No great surprises so far. 106 00:04:56,040 --> 00:04:57,333 All good so far.