1 00:00:05,980 --> 00:00:13,870 Crates are about code sharing between projects, while modules are about code sharing within a project. 2 00:00:14,080 --> 00:00:19,900 Modules are a collection of items such as named features like structs and functions. 3 00:00:20,080 --> 00:00:26,740 We can create a module using the mod keyword, but let's start off with creating our first library. 4 00:00:27,370 --> 00:00:32,830 To do this, we type in cargo new like always, and then the name we want our crate to be. 5 00:00:32,830 --> 00:00:39,340 And then we add tic tac lib to it to specify that we want to create a library. 6 00:00:39,520 --> 00:00:46,060 So let's change into that directory and let's look what's inside the source folder, which as I mentioned 7 00:00:46,060 --> 00:00:49,970 in the previous lecture, is going to be lib rs. 8 00:00:51,070 --> 00:00:57,250 So we can delete all the contact contents out of here and we can declare our first module, which we 9 00:00:57,250 --> 00:01:01,210 will call mod list and then open and close brackets. 10 00:01:01,390 --> 00:01:14,290 And here we can create additional modules like mod things to do, mod items completed. 11 00:01:15,340 --> 00:01:20,610 So now we have two modules inside of our module. 12 00:01:20,830 --> 00:01:23,500 So now let's add a struct called task. 13 00:01:26,530 --> 00:01:34,000 And then in here, we're going to have an item that is of type string and then let's add some functions 14 00:01:34,000 --> 00:01:36,190 inside of our things to do. 15 00:01:36,190 --> 00:01:39,250 So we'll say add activity, 16 00:01:42,730 --> 00:01:44,800 update activity 17 00:01:47,080 --> 00:01:50,740 and marked completed. 18 00:01:51,880 --> 00:02:03,490 And then let's add a couple down to items completed such as remove task and move back to do. 19 00:02:05,490 --> 00:02:13,980 So a useful tool that we can install is cargo modules and we can install it by running cargo and small 20 00:02:14,010 --> 00:02:16,680 cargo modules. 21 00:02:16,680 --> 00:02:23,220 And I already have this installed, so pause the video while you wait for yours to install, but once 22 00:02:23,220 --> 00:02:28,320 it is installed you can start running a command as such cargo. 23 00:02:30,060 --> 00:02:34,170 Modules generate tree. 24 00:02:35,470 --> 00:02:39,910 And what this is going to do is it's going to display to us the. 25 00:02:40,990 --> 00:02:48,460 Set up of our crate with the modules inside of it, but we can also make it a little better by adding 26 00:02:48,460 --> 00:02:49,930 with types to it. 27 00:02:50,770 --> 00:02:55,600 And now we can see our structs and our functions that are inside of our modules. 28 00:02:56,620 --> 00:03:00,130 And then here you can see the pub keyword a lot. 29 00:03:00,130 --> 00:03:06,190 The pub keyword makes an item public so it can be accessed from outside the module. 30 00:03:06,940 --> 00:03:14,590 One function is marked pub crate and that is so that it is available anywhere inside the crate, but 31 00:03:14,590 --> 00:03:17,830 it isn't exposed to anything external to the crate. 32 00:03:17,830 --> 00:03:22,180 So in this case, our pub crate is our list module. 33 00:03:23,280 --> 00:03:30,480 If there is no pub keyword then the item is pub self, which we can see pretty much everywhere currently. 34 00:03:30,900 --> 00:03:39,510 Which pub self is equivalent to private and other languages and it is the default choice when we are 35 00:03:39,510 --> 00:03:43,200 implementing a function or a struct or whatever else there might be. 36 00:03:43,680 --> 00:03:49,870 So if we wanted to make it public, then simply just add the pub keyword. 37 00:03:49,890 --> 00:03:59,550 So if we rerun our tree with types, we can now see that the task struck is now public and we have a 38 00:03:59,550 --> 00:04:06,990 green pub word to differentiate itself from pub self, which again means private. 39 00:04:08,250 --> 00:04:14,940 So let's add a function down here and see how we can access some of these functions in the struc inside 40 00:04:14,940 --> 00:04:15,870 of our modules. 41 00:04:15,870 --> 00:04:29,400 So let's call it let's add task and then we'll say let task equal list task. 42 00:04:31,610 --> 00:04:41,660 Tags and then we'll say item string from task. 43 00:04:43,400 --> 00:04:48,380 Okay, so we got a red squiggly and let's cargo build it. 44 00:04:48,380 --> 00:04:50,510 That way we can see what our air is going to be. 45 00:04:50,510 --> 00:04:54,350 So it is telling us that the item is a private field. 46 00:04:54,350 --> 00:05:01,340 So even though we made tasks public, we have to make the fields inside of it also public. 47 00:05:01,340 --> 00:05:06,890 So now if we cargo build it just to make sure the error is gone, we're now good with that. 48 00:05:07,010 --> 00:05:09,890 So let's try to access some of our functions. 49 00:05:09,890 --> 00:05:16,670 So we'll say list things to do, add activity. 50 00:05:17,540 --> 00:05:21,860 And the first thing that we see is that things to do is private. 51 00:05:21,860 --> 00:05:27,020 So put pub in front of it and then we see add activity gets it. 52 00:05:27,020 --> 00:05:31,130 So we have to make that function public as well. 53 00:05:31,340 --> 00:05:38,570 And the way that we did this is called the relative path because we said, hey, go to the list module 54 00:05:39,080 --> 00:05:43,310 and then go to things to do and then go to add activity. 55 00:05:43,580 --> 00:05:49,640 But if we want to do our absolute path, then we would start it off with the crate keyword and then 56 00:05:49,640 --> 00:05:53,270 follow it the same way. 57 00:05:53,270 --> 00:05:56,090 And this is the absolute path 58 00:05:59,300 --> 00:06:02,780 because we start at the. 59 00:06:03,830 --> 00:06:09,530 Root crate, which is designated here by this crate keyword. 60 00:06:10,250 --> 00:06:17,660 But you can imagine that as a project grows, we wouldn't want just this one lib RC file to contain 61 00:06:17,660 --> 00:06:18,710 every module. 62 00:06:19,010 --> 00:06:24,770 That'd be a nightmare to maintain, but luckily we can export modules to different files. 63 00:06:25,740 --> 00:06:31,170 So what we're going to do is we're going to first start off by exporting out the things to do module. 64 00:06:31,410 --> 00:06:38,700 So inside source folder, we're going to create a new file called Things to Do. 65 00:06:39,140 --> 00:06:43,470 RS And then we're going to take the functions out of here. 66 00:06:44,800 --> 00:06:50,680 Go to things to do, paste them in, and then we can remove this entire block of code. 67 00:06:52,860 --> 00:07:01,550 So now we need to add a couple of things we need to say mod things to do and that's going to tell rust, 68 00:07:01,560 --> 00:07:05,100 hey, I want you to look for a things to do. 69 00:07:06,650 --> 00:07:07,190 File. 70 00:07:07,190 --> 00:07:10,820 So it's going to have to be called exactly things to do. 71 00:07:11,030 --> 00:07:15,260 And now what we can do is we can come down here and remove list. 72 00:07:16,940 --> 00:07:20,420 Now everything is back to the way that it was. 73 00:07:21,890 --> 00:07:28,130 So it would kind of be a pain if every time we wanted to call something inside of things to do, have 74 00:07:28,130 --> 00:07:29,950 to type out things to do. 75 00:07:29,960 --> 00:07:37,400 So what we can actually do here, so say we want to be able to call add activity is we can say use, 76 00:07:37,430 --> 00:07:42,110 create things to do and then add activity. 77 00:07:43,070 --> 00:07:45,650 And now we can remove all of this. 78 00:07:46,510 --> 00:07:50,770 As you can see, they are now exactly equivalent. 79 00:07:50,770 --> 00:07:56,290 So now we can actually remove this one and now we're good to go. 80 00:07:57,070 --> 00:07:59,420 But how about nested modules? 81 00:07:59,440 --> 00:08:02,170 How can we split those into different files? 82 00:08:02,410 --> 00:08:04,630 Well, let's create a new folder. 83 00:08:06,570 --> 00:08:09,300 Called Things to do. 84 00:08:11,550 --> 00:08:16,950 And then when we use mod things to do, rust is going to look for things to do. 85 00:08:17,700 --> 00:08:19,140 Which we already created. 86 00:08:19,140 --> 00:08:26,700 And then all the sub modules will appear in the things to do directory and cargo will know to look there 87 00:08:26,700 --> 00:08:30,720 for sub modules as long as we add in here. 88 00:08:30,750 --> 00:08:36,870 Pub mod name of module. 89 00:08:40,150 --> 00:08:42,460 So let's go ahead and do that. 90 00:08:42,460 --> 00:08:43,480 Let's move. 91 00:08:44,050 --> 00:08:45,460 Items completed. 92 00:08:47,180 --> 00:08:47,960 Into. 93 00:08:49,350 --> 00:08:50,670 Things to do directory. 94 00:08:50,670 --> 00:08:59,630 So inside of there let's create a new file called Items Completed RS. 95 00:09:01,220 --> 00:09:05,510 And then we'll go back in here and we're going to take these. 96 00:09:06,720 --> 00:09:08,190 Place it inside of here. 97 00:09:08,190 --> 00:09:12,530 And then just so that we can access at least one of them, we'll make it public. 98 00:09:12,540 --> 00:09:17,880 And then our name of our module is now items completed. 99 00:09:26,010 --> 00:09:27,780 We can come back into here. 100 00:09:29,380 --> 00:09:32,170 And we can remove this. 101 00:09:34,050 --> 00:09:35,760 And we can add in. 102 00:09:38,060 --> 00:09:40,490 Use things. 103 00:09:43,660 --> 00:09:43,830 Okay. 104 00:09:44,830 --> 00:09:46,060 Things to do. 105 00:09:46,630 --> 00:09:48,040 Items completed. 106 00:09:49,300 --> 00:09:57,310 And then here we can now reference items completed remove task, which is what we made public in this 107 00:09:57,310 --> 00:09:57,820 case. 108 00:09:59,150 --> 00:10:02,690 So and then everything else is basically the same now. 109 00:10:02,690 --> 00:10:10,340 So if we wanted to, we can come inside items completed say pub mod test to create a new module called 110 00:10:10,340 --> 00:10:13,640 Test add in a test function. 111 00:10:15,090 --> 00:10:24,150 And then we can go back into libraries and say, use things to do items completed. 112 00:10:26,380 --> 00:10:27,310 Test. 113 00:10:28,790 --> 00:10:29,570 Test. 114 00:10:29,780 --> 00:10:31,310 And now we can just call. 115 00:10:34,440 --> 00:10:39,950 Test like normal and that's pretty much all there is to it, to modules. 116 00:10:39,960 --> 00:10:46,980 It is a bit unique against other languages, but it is pretty simple once you understand how cargo likes 117 00:10:46,980 --> 00:10:48,600 the structure of it to be. 118 00:10:48,870 --> 00:10:57,930 And now let's just take a look at our new generate tree with types and we can see that we have our public 119 00:10:57,960 --> 00:11:04,920 task struck a couple of public functions, a public module called items completed. 120 00:11:05,860 --> 00:11:13,240 With a couple of more functions in it that are public as well as our other module and the function that 121 00:11:13,240 --> 00:11:14,370 we just created. 122 00:11:14,380 --> 00:11:20,680 So again, I think that cargo modules generate tree is a very powerful tool to help us visualize our 123 00:11:20,680 --> 00:11:21,480 structure. 124 00:11:21,490 --> 00:11:27,040 So hopefully it helps you out as you take your journey into figuring out modules.