1 00:00:00,270 --> 00:00:02,800 Systems Programming in Rust, best 2 00:00:02,800 --> 00:00:05,709 practices. In this video, we will discuss 3 00:00:05,709 --> 00:00:08,380 the practice of creating a library and 4 00:00:08,380 --> 00:00:11,440 organizing it into modules. The root of 5 00:00:11,440 --> 00:00:14,410 your library is Lib dot RS, located in the 6 00:00:14,410 --> 00:00:16,990 source directory. A module in your 7 00:00:16,990 --> 00:00:19,779 library is defined by using some form of 8 00:00:19,779 --> 00:00:21,699 mod and then the module name. 9 00:00:21,699 --> 00:00:24,070 If you end that with a semicolon, then 10 00:00:24,070 --> 00:00:26,140 cargo will search for a file with the 11 00:00:26,140 --> 00:00:29,050 same name as the module in the same 12 00:00:29,050 --> 00:00:32,168 directory as Lib dot RS. Okay, but what 13 00:00:32,168 --> 00:00:36,040 about nested modules? If inside my module 14 00:00:36,040 --> 00:00:39,430 we define a nested module called my sub 15 00:00:39,430 --> 00:00:42,160 module, that will be found in a directory 16 00:00:42,160 --> 00:00:44,710 with the same name as my module, in a 17 00:00:44,710 --> 00:00:47,950 file name called my sub module RS. This 18 00:00:47,950 --> 00:00:50,260 nesting pattern can be repeated over and 19 00:00:50,260 --> 00:00:52,750 over to an arbitrary depth. With a 20 00:00:52,750 --> 00:00:55,510 structure like this, you can bring the 21 00:00:55,510 --> 00:00:58,329 items inside of Lib dot RS into scope by 22 00:00:58,329 --> 00:01:01,630 using my project. You can bring the items 23 00:01:01,630 --> 00:01:04,870 in my module into scope by using my 24 00:01:04,870 --> 00:01:07,780 project, my module, and you can bring the 25 00:01:07,780 --> 00:01:10,270 items into scope from my sub module dot 26 00:01:10,270 --> 00:01:14,440 RS, by using my project my module my sub 27 00:01:14,440 --> 00:01:17,380 module. This is all great but we only use 28 00:01:17,380 --> 00:01:20,530 libraries from some executable binaries 29 00:01:20,530 --> 00:01:22,840 somewhere, so let's also go over the two 30 00:01:22,840 --> 00:01:24,640 main ways that you can create a binary. 31 00:01:24,640 --> 00:01:27,130 The first way is the way that we've been 32 00:01:27,130 --> 00:01:29,799 using this whole course, and that's just 33 00:01:29,799 --> 00:01:31,990 to have a main dot RS in the source 34 00:01:31,990 --> 00:01:34,360 directory. The name of the binary this 35 00:01:34,360 --> 00:01:36,610 produces is the same as the name of your 36 00:01:36,610 --> 00:01:39,280 project which is my project in this 37 00:01:39,280 --> 00:01:42,010 example. The other main way to specify a 38 00:01:42,010 --> 00:01:45,130 binary is to place a file inside of the 39 00:01:45,130 --> 00:01:48,100 bin subdirectory of source. The name of 40 00:01:48,100 --> 00:01:49,869 the executable will be taken from the 41 00:01:49,869 --> 00:01:52,510 name of the file. So in this example the 42 00:01:52,510 --> 00:01:54,520 binaries bar, the benefit of this 43 00:01:54,520 --> 00:01:57,180 approach is that you can add as many 44 00:01:57,180 --> 00:01:59,619 executables as you want. If you do this 45 00:01:59,619 --> 00:02:01,409 you need to remember to specify which 46 00:02:01,409 --> 00:02:04,150 binary to run on the command-line when 47 00:02:04,150 --> 00:02:06,729 you do your Cargo run command. So if we 48 00:02:06,729 --> 00:02:08,470 put it all together a typical project 49 00:02:08,470 --> 00:02:11,229 might look like this. So let's recap, this 50 00:02:11,229 --> 00:02:13,690 project has two binaries, bar 51 00:02:13,690 --> 00:02:17,800 and baz, it has a library, it has a module 52 00:02:17,800 --> 00:02:21,220 called my module, and it has a sub module 53 00:02:21,220 --> 00:02:24,520 of my module called my sub module. We 54 00:02:24,520 --> 00:02:27,610 have no use for an extra binary in this 55 00:02:27,610 --> 00:02:30,580 project, and we've already made good use 56 00:02:30,580 --> 00:02:33,490 of library modules, so let's go 57 00:02:33,490 --> 00:02:35,860 demonstrate creating a library sub 58 00:02:35,860 --> 00:02:38,380 module. The first thing we need to do is 59 00:02:38,380 --> 00:02:40,300 tell our stats module we want a sub 60 00:02:40,300 --> 00:02:42,880 module named timer. Next we need to go 61 00:02:42,880 --> 00:02:45,430 create the directory and file for the 62 00:02:45,430 --> 00:02:47,710 timer to be in. The directory is stats 63 00:02:47,710 --> 00:02:49,360 because it's the same as the stats 64 00:02:49,360 --> 00:02:52,270 module name and then timer dot RS, it 65 00:02:52,270 --> 00:02:54,520 starts empty of course, so let's go back 66 00:02:54,520 --> 00:02:57,280 over to stats and go down and grab our 67 00:02:57,280 --> 00:02:59,940 timer struct definition and 68 00:02:59,940 --> 00:03:02,830 implementation. So here's our data fields 69 00:03:02,830 --> 00:03:05,590 and our implementation, cut those, and go 70 00:03:05,590 --> 00:03:09,160 paste them into timer. Next, we need to go 71 00:03:09,160 --> 00:03:11,440 do our imports so we can see these items, 72 00:03:11,440 --> 00:03:13,840 so I will just auto import some of these 73 00:03:13,840 --> 00:03:16,180 using the IDE, if you go up you can see 74 00:03:16,180 --> 00:03:18,730 it created that for us. I think that's 75 00:03:18,730 --> 00:03:21,010 all we need. Next we need to go make some 76 00:03:21,010 --> 00:03:23,350 things public so that we can access them, 77 00:03:23,350 --> 00:03:25,510 pretty much everything is public in here, 78 00:03:25,510 --> 00:03:28,570 so timer is public, all of these fields 79 00:03:28,570 --> 00:03:31,570 are public, and the function and the 80 00:03:31,570 --> 00:03:34,090 method that we implement are also public. 81 00:03:34,090 --> 00:03:36,959 So we'll just finish that up and save. 82 00:03:36,959 --> 00:03:40,930 That should be good to go. Let's go over 83 00:03:40,930 --> 00:03:44,560 here and check everything, a little bit 84 00:03:44,560 --> 00:03:47,620 of red here timer itself is now in a sub 85 00:03:47,620 --> 00:03:51,790 module. So let's add our use statement 86 00:03:51,790 --> 00:03:53,920 for timer. If we did everything right 87 00:03:53,920 --> 00:03:57,870 this should work exactly like before, so 88 00:03:57,870 --> 00:04:02,400 piping our hello to Cargo, 89 00:04:02,400 --> 00:04:05,549 we get a warning, okay so let's go clean 90 00:04:05,549 --> 00:04:08,250 up that warning, we don't use duration 91 00:04:08,250 --> 00:04:10,140 anymore in this module, so we'll just 92 00:04:10,140 --> 00:04:12,569 take that out, fix the brace, and then we 93 00:04:12,569 --> 00:04:14,939 can go back and try again. Let's echo 94 00:04:14,939 --> 00:04:17,100 hello to Cargo run once again, no 95 00:04:17,100 --> 00:04:19,529 warnings this time great, just to run our 96 00:04:19,529 --> 00:04:23,519 yes just for fun, okay great 97 00:04:23,519 --> 00:04:24,750 looks like everything is working 98 00:04:24,750 --> 00:04:28,380 normally. In the next video, I'll show you 99 00:04:28,380 --> 00:04:33,380 how to write and run tests on your code.