1 00:00:06,660 --> 00:00:09,120 - The Rust community provides a wide range 2 00:00:09,120 --> 00:00:12,540 of crates to support all kinds of program initiative, 3 00:00:12,540 --> 00:00:16,710 such as accessing databases, developing web applications, 4 00:00:16,710 --> 00:00:19,470 analyzing data, and much more beside. 5 00:00:19,470 --> 00:00:22,590 So to get a insight into what crates are available, 6 00:00:22,590 --> 00:00:26,730 you can go to the Rust crate registry online, crates.io. 7 00:00:26,730 --> 00:00:27,630 Let's have a look. 8 00:00:28,710 --> 00:00:32,100 Okay, so this is crates.io. 9 00:00:32,100 --> 00:00:34,080 You can search in the text box 10 00:00:34,080 --> 00:00:35,850 to find out what crates might be available 11 00:00:35,850 --> 00:00:38,220 for a particular aspect of programming. 12 00:00:38,220 --> 00:00:43,020 So for example, is there a crate, a library in Rust 13 00:00:43,020 --> 00:00:45,780 that will allow me to talk to a MySQL database? 14 00:00:45,780 --> 00:00:46,773 Obviously, yes. 15 00:00:47,610 --> 00:00:49,067 And here it is, mysql. 16 00:00:50,430 --> 00:00:53,910 Okay, so basically, you need to add this crate 17 00:00:53,910 --> 00:00:57,270 into your project to give you the ability to talk 18 00:00:57,270 --> 00:00:58,773 to a MySQL database. 19 00:01:00,390 --> 00:01:03,130 Here's the documentation for mysql 20 00:01:04,050 --> 00:01:05,850 and then we can scroll through here. 21 00:01:06,720 --> 00:01:10,500 And it says, in order to use this crate in your application, 22 00:01:10,500 --> 00:01:15,450 just add it as a dependency in your TOML, mysql star. 23 00:01:15,450 --> 00:01:18,180 Or, of course, you could give it a particular version number 24 00:01:18,180 --> 00:01:22,590 if you wanted to, but that should be sufficient. 25 00:01:22,590 --> 00:01:23,940 So that's what we're going to do. 26 00:01:23,940 --> 00:01:27,210 We're going to add the MySQL crate to our application. 27 00:01:27,210 --> 00:01:28,980 It'll enable our application to connect 28 00:01:28,980 --> 00:01:32,520 to a MySQL database and to execute queries. 29 00:01:32,520 --> 00:01:33,870 So guess what? 30 00:01:33,870 --> 00:01:36,630 We've added this dependency into our TOML file. 31 00:01:36,630 --> 00:01:38,910 Let's take a look at that in our project. 32 00:01:38,910 --> 00:01:41,580 So here's my project, and in the TOML file, 33 00:01:41,580 --> 00:01:45,930 you'll notice it includes the mysql dependency. 34 00:01:45,930 --> 00:01:49,140 So the first time you run the application, 35 00:01:49,140 --> 00:01:52,650 it'll pull this dependency into your local folder. 36 00:01:52,650 --> 00:01:53,583 So let's do that. 37 00:01:58,110 --> 00:01:59,493 Cargo run. 38 00:02:01,380 --> 00:02:02,670 When I do a cargo run, 39 00:02:02,670 --> 00:02:06,900 it's also going to run all the code in the application. 40 00:02:06,900 --> 00:02:09,333 I think what I'm gonna do first, in main, 41 00:02:11,070 --> 00:02:15,420 I'm going to comment out the substantial code 42 00:02:15,420 --> 00:02:18,510 because that would just be spoiling the fun. 43 00:02:18,510 --> 00:02:20,640 So I'll just comment out the code in main 44 00:02:20,640 --> 00:02:22,470 that's actually going to do anything. 45 00:02:22,470 --> 00:02:23,430 All I wanna do at this stage 46 00:02:23,430 --> 00:02:27,060 is just to pull down the mysql dependencies. 47 00:02:27,060 --> 00:02:29,460 So when you run a cargo application for the first time, 48 00:02:29,460 --> 00:02:31,230 it looks in your cargo TOML, 49 00:02:31,230 --> 00:02:33,000 it sees what dependencies you've mentioned 50 00:02:33,000 --> 00:02:34,470 and it pulls them down. 51 00:02:34,470 --> 00:02:37,560 And often when you pull down one dependency like MySQL, 52 00:02:37,560 --> 00:02:39,510 it will have a whole series of sub-dependencies, 53 00:02:39,510 --> 00:02:41,940 which also need to be downloaded. 54 00:02:41,940 --> 00:02:44,790 So these dependencies here all came from the fact 55 00:02:44,790 --> 00:02:47,910 that I basically specified the mysql dependency. 56 00:02:47,910 --> 00:02:50,220 It pulled down a whole load of other dependencies, 57 00:02:50,220 --> 00:02:52,590 which can themselves have other dependencies. 58 00:02:52,590 --> 00:02:54,600 So it goes on a little while 59 00:02:54,600 --> 00:02:57,210 and then eventually, it'll finish. 60 00:02:57,210 --> 00:02:58,530 So I'm just gonna leave that go. 61 00:02:58,530 --> 00:03:00,690 Gonna leave it go in the background. 62 00:03:00,690 --> 00:03:02,040 So as we've just seen, 63 00:03:02,040 --> 00:03:03,540 the first time we run the application, 64 00:03:03,540 --> 00:03:06,000 cargo downloads the dependency MySQL 65 00:03:06,000 --> 00:03:07,350 onto your local machine, 66 00:03:07,350 --> 00:03:09,720 into your local folder, in fact, plus a whole load 67 00:03:09,720 --> 00:03:12,510 of other related dependencies as well. 68 00:03:12,510 --> 00:03:16,350 So in the MySQL crate, there are many structures 69 00:03:16,350 --> 00:03:18,810 and functions and enums and traits, 70 00:03:18,810 --> 00:03:22,410 which enable us to access a MySQL database. 71 00:03:22,410 --> 00:03:26,430 If you want the full documentation, then it's available. 72 00:03:26,430 --> 00:03:31,430 Go into docs.rs, it's the mysql we're looking for. 73 00:03:31,980 --> 00:03:33,720 So let's have a look there. 74 00:03:33,720 --> 00:03:36,540 That'll give us the documentation about what types, 75 00:03:36,540 --> 00:03:38,880 what functions, what enums, et cetera 76 00:03:38,880 --> 00:03:41,520 are defined in the mysql crate 77 00:03:41,520 --> 00:03:43,320 that we can then access in our code. 78 00:03:44,250 --> 00:03:47,700 So this is the documentation for the mysql crate 79 00:03:47,700 --> 00:03:51,990 and it has some kind of introductory information here. 80 00:03:51,990 --> 00:03:54,660 And it says, in order to use this crate in your application, 81 00:03:54,660 --> 00:03:56,730 just add it into your dependencies. 82 00:03:56,730 --> 00:03:58,440 Well, we've seen that. 83 00:03:58,440 --> 00:04:01,110 It's got a little example bit of code here. 84 00:04:01,110 --> 00:04:03,990 Our example is gonna be fairly similar to that. 85 00:04:03,990 --> 00:04:06,690 It also mentions all the different types 86 00:04:06,690 --> 00:04:08,430 and macros and enums and constants 87 00:04:08,430 --> 00:04:12,900 and functions which are defined in the mysql crate. 88 00:04:12,900 --> 00:04:16,950 So structs, well, quite a lot actually. 89 00:04:16,950 --> 00:04:18,120 Lots of different structures. 90 00:04:18,120 --> 00:04:20,790 Not surprisingly, you can imagine talking to a database, 91 00:04:20,790 --> 00:04:22,323 there is quite a big API. 92 00:04:23,310 --> 00:04:26,610 So for example, conn represents a connection. 93 00:04:26,610 --> 00:04:28,740 We'll have at that shortly. 94 00:04:28,740 --> 00:04:32,490 ResultSet, a bunch of rows that you get back. 95 00:04:32,490 --> 00:04:35,520 Each row is one row from the database. 96 00:04:35,520 --> 00:04:36,600 Transaction. 97 00:04:36,600 --> 00:04:38,490 You can run your code in the transaction 98 00:04:38,490 --> 00:04:40,110 and commit or roll back. 99 00:04:40,110 --> 00:04:43,560 So if you've done any work in any database context before, 100 00:04:43,560 --> 00:04:44,700 you'll kind of recognize the need 101 00:04:44,700 --> 00:04:46,410 for some of these structure types. 102 00:04:46,410 --> 00:04:50,250 And then enums, there are enums to represent different types 103 00:04:50,250 --> 00:04:51,630 of error that can occur. 104 00:04:51,630 --> 00:04:55,920 That's quite, not to be surprised at that really. 105 00:04:55,920 --> 00:04:58,263 What else might we look at? Macros. 106 00:04:59,730 --> 00:05:02,550 So there's a params macro. That's handy. 107 00:05:02,550 --> 00:05:07,050 A handy way to pass named parameters into a statement. 108 00:05:07,050 --> 00:05:07,980 Bear that in mind. 109 00:05:07,980 --> 00:05:09,580 We're gonna be using that later. 110 00:05:10,440 --> 00:05:13,470 So in order to use those types in your application, 111 00:05:13,470 --> 00:05:16,560 you just need to import the mysql module 112 00:05:16,560 --> 00:05:19,500 and also mysql::prelude. 113 00:05:19,500 --> 00:05:22,170 So I've imported, I've basically made my life easy. 114 00:05:22,170 --> 00:05:27,170 I've imported all named entities from the mysql module 115 00:05:28,260 --> 00:05:32,673 and from the prelude module as well in main.rs. 116 00:05:33,630 --> 00:05:37,800 So here in main.rs, if I go to the top, ooh, 117 00:05:37,800 --> 00:05:42,060 right to the top, let me just move this down a little bit. 118 00:05:42,060 --> 00:05:45,390 Notice it's still actually busy pulling down dependencies. 119 00:05:45,390 --> 00:05:46,620 I think I'm gonna come back in a month 120 00:05:46,620 --> 00:05:47,880 and see if it's finished. 121 00:05:47,880 --> 00:05:50,130 So I've imported the types module, 122 00:05:50,130 --> 00:05:51,780 that's my types module there. 123 00:05:51,780 --> 00:05:53,370 We'll have a look at that in a moment. 124 00:05:53,370 --> 00:05:57,960 And then I've imported the mysql module, 125 00:05:57,960 --> 00:06:00,450 everything from there just to make my application easier. 126 00:06:00,450 --> 00:06:02,370 So for example, the conn structure 127 00:06:02,370 --> 00:06:06,510 for connection and also prelude like so. 128 00:06:06,510 --> 00:06:08,730 And then also I've imported, 129 00:06:08,730 --> 00:06:10,830 I've got a structure type called Employee. 130 00:06:12,960 --> 00:06:17,310 So our model class is going to be representing one employee. 131 00:06:17,310 --> 00:06:20,100 I'm gonna be storing employees in the database. 132 00:06:20,100 --> 00:06:23,670 So I've defined the employee in types.rs. 133 00:06:23,670 --> 00:06:25,320 So should we have a look at that? 134 00:06:26,310 --> 00:06:29,730 Okay, so here is my employee structure. 135 00:06:29,730 --> 00:06:32,190 In terms of data, it's quite straightforward. 136 00:06:32,190 --> 00:06:34,770 Oh, and also for the sake of simplicity, 137 00:06:34,770 --> 00:06:37,650 I wanted to make this example as simple as I could. 138 00:06:37,650 --> 00:06:39,250 I've actually got public fields. 139 00:06:40,140 --> 00:06:44,400 In reality, you'd have private fields and public functions 140 00:06:44,400 --> 00:06:46,260 but I could do that. 141 00:06:46,260 --> 00:06:48,570 I just wanted to focus on the database access. 142 00:06:48,570 --> 00:06:52,140 So this employee structure, 143 00:06:52,140 --> 00:06:55,020 it's kind of like an entity class in other APIs. 144 00:06:55,020 --> 00:06:57,123 It kind of maps to the table structure. 145 00:06:58,560 --> 00:07:01,800 The employee_id, a 32-bit integer. 146 00:07:01,800 --> 00:07:03,930 An employee has a name and a salary 147 00:07:03,930 --> 00:07:07,200 and a region where they work, like London. 148 00:07:07,200 --> 00:07:09,840 And in terms of implementation, I've got a function, 149 00:07:09,840 --> 00:07:12,000 which will basically create a new instance. 150 00:07:12,000 --> 00:07:13,860 It's like a factory function. 151 00:07:13,860 --> 00:07:15,540 So it's an associated function 152 00:07:15,540 --> 00:07:19,320 where I'd say Employee new, I'd pass in the id. 153 00:07:19,320 --> 00:07:22,440 So if I read a row of data from the database, 154 00:07:22,440 --> 00:07:24,213 I can pass that data into here. 155 00:07:25,140 --> 00:07:27,870 And what it'll read to me back is a new Employee object 156 00:07:27,870 --> 00:07:29,283 containing that data. 157 00:07:30,450 --> 00:07:31,283 So that's what I'm going to do. 158 00:07:31,283 --> 00:07:34,140 I'm gonna fetch data a row at a time from the database. 159 00:07:34,140 --> 00:07:38,910 Each row, I'm going to create an Employee instance from it. 160 00:07:38,910 --> 00:07:43,590 And also, I've implemented the Display trait. 161 00:07:43,590 --> 00:07:44,910 Remember the Display trait? 162 00:07:44,910 --> 00:07:47,550 It allows you to output an Employee object. 163 00:07:47,550 --> 00:07:50,340 You could say something like this, and println macro. 164 00:07:53,070 --> 00:07:54,630 Oh, my application seems 165 00:07:54,630 --> 00:07:57,180 to have reached a point of conclusion. 166 00:07:57,180 --> 00:07:59,580 I'll come back to look at those warnings in a minute. 167 00:07:59,580 --> 00:08:02,340 I could output, using the default formatter, 168 00:08:02,340 --> 00:08:05,940 I could output an Employee object like that. 169 00:08:05,940 --> 00:08:08,670 And the default formatter, as we know, it relies 170 00:08:08,670 --> 00:08:12,420 on the Display trait and in particular, the format function. 171 00:08:12,420 --> 00:08:15,630 So that would be an employee and it can be formatted 172 00:08:15,630 --> 00:08:18,210 to return back basically a string. 173 00:08:18,210 --> 00:08:21,180 And when I write using the formatter, 174 00:08:21,180 --> 00:08:23,670 the string that it returns, which will be displayed 175 00:08:23,670 --> 00:08:27,270 in my application will just display the employee's ID, name, 176 00:08:27,270 --> 00:08:29,250 salary and region. 177 00:08:29,250 --> 00:08:31,380 So I'd call that a model class. 178 00:08:31,380 --> 00:08:34,890 Like I said, similar to an entity class if you're familiar 179 00:08:34,890 --> 00:08:39,540 with Hibernate or JPA or Entity Framework in .NET. 180 00:08:39,540 --> 00:08:40,650 Similar kind of idea. 181 00:08:40,650 --> 00:08:44,280 There's no direct mapping from the table structure 182 00:08:44,280 --> 00:08:45,870 to my employee data. 183 00:08:45,870 --> 00:08:48,840 It's just by convenience, I've decided 184 00:08:48,840 --> 00:08:51,870 to make it a similar kind of data structure. 185 00:08:51,870 --> 00:08:54,540 So let's just have a quick look at these warnings down here. 186 00:08:54,540 --> 00:08:57,390 When I ran the application, I spent an eternity 187 00:08:57,390 --> 00:09:00,660 pulling down the mysql dependency plus the sub dependencies 188 00:09:00,660 --> 00:09:02,280 and eventually, it finished 189 00:09:02,280 --> 00:09:04,140 and then it's given me a bunch of warnings. 190 00:09:04,140 --> 00:09:06,780 Hardly surprising really because I've got a whole bunch 191 00:09:06,780 --> 00:09:10,680 of functions defined everywhere that I'm not actually using. 192 00:09:10,680 --> 00:09:12,090 The reason for those warnings, of course, 193 00:09:12,090 --> 00:09:16,200 is because in my main, I commented out all the code. 194 00:09:16,200 --> 00:09:18,810 So no wonder it's complaining about unused things 195 00:09:18,810 --> 00:09:21,540 because I'm not actually calling any code anywhere. 196 00:09:21,540 --> 00:09:24,090 So if i just uncomment that code, 197 00:09:24,090 --> 00:09:26,400 then I'm not gonna run the application again just yet 198 00:09:26,400 --> 00:09:29,463 but it will now be in a fit state to run in the next demo.