- The Rust community provides a wide range
of crates to support all kinds of program initiative,
such as accessing databases, developing web applications,
analyzing data, and much more beside.
So to get a insight into what crates are available,
you can go to the Rust crate registry online, crates.io.
Let's have a look.
Okay, so this is crates.io.
You can search in the text box
to find out what crates might be available
for a particular aspect of programming.
So for example, is there a crate, a library in Rust
that will allow me to talk to a MySQL database?
Obviously, yes.
And here it is, mysql.
Okay, so basically, you need to add this crate
into your project to give you the ability to talk
to a MySQL database.
Here's the documentation for mysql
and then we can scroll through here.
And it says, in order to use this crate in your application,
just add it as a dependency in your TOML, mysql star.
Or, of course, you could give it a particular version number
if you wanted to, but that should be sufficient.
So that's what we're going to do.
We're going to add the MySQL crate to our application.
It'll enable our application to connect
to a MySQL database and to execute queries.
So guess what?
We've added this dependency into our TOML file.
Let's take a look at that in our project.
So here's my project, and in the TOML file,
you'll notice it includes the mysql dependency.
So the first time you run the application,
it'll pull this dependency into your local folder.
So let's do that.
Cargo run.
When I do a cargo run,
it's also going to run all the code in the application.
I think what I'm gonna do first, in main,
I'm going to comment out the substantial code
because that would just be spoiling the fun.
So I'll just comment out the code in main
that's actually going to do anything.
All I wanna do at this stage
is just to pull down the mysql dependencies.
So when you run a cargo application for the first time,
it looks in your cargo TOML,
it sees what dependencies you've mentioned
and it pulls them down.
And often when you pull down one dependency like MySQL,
it will have a whole series of sub-dependencies,
which also need to be downloaded.
So these dependencies here all came from the fact
that I basically specified the mysql dependency.
It pulled down a whole load of other dependencies,
which can themselves have other dependencies.
So it goes on a little while
and then eventually, it'll finish.
So I'm just gonna leave that go.
Gonna leave it go in the background.
So as we've just seen,
the first time we run the application,
cargo downloads the dependency MySQL
onto your local machine,
into your local folder, in fact, plus a whole load
of other related dependencies as well.
So in the MySQL crate, there are many structures
and functions and enums and traits,
which enable us to access a MySQL database.
If you want the full documentation, then it's available.
Go into docs.rs, it's the mysql we're looking for.
So let's have a look there.
That'll give us the documentation about what types,
what functions, what enums, et cetera
are defined in the mysql crate
that we can then access in our code.
So this is the documentation for the mysql crate
and it has some kind of introductory information here.
And it says, in order to use this crate in your application,
just add it into your dependencies.
Well, we've seen that.
It's got a little example bit of code here.
Our example is gonna be fairly similar to that.
It also mentions all the different types
and macros and enums and constants
and functions which are defined in the mysql crate.
So structs, well, quite a lot actually.
Lots of different structures.
Not surprisingly, you can imagine talking to a database,
there is quite a big API.
So for example, conn represents a connection.
We'll have at that shortly.
ResultSet, a bunch of rows that you get back.
Each row is one row from the database.
Transaction.
You can run your code in the transaction
and commit or roll back.
So if you've done any work in any database context before,
you'll kind of recognize the need
for some of these structure types.
And then enums, there are enums to represent different types
of error that can occur.
That's quite, not to be surprised at that really.
What else might we look at? Macros.
So there's a params macro. That's handy.
A handy way to pass named parameters into a statement.
Bear that in mind.
We're gonna be using that later.
So in order to use those types in your application,
you just need to import the mysql module
and also mysql::prelude.
So I've imported, I've basically made my life easy.
I've imported all named entities from the mysql module
and from the prelude module as well in main.rs.
So here in main.rs, if I go to the top, ooh,
right to the top, let me just move this down a little bit.
Notice it's still actually busy pulling down dependencies.
I think I'm gonna come back in a month
and see if it's finished.
So I've imported the types module,
that's my types module there.
We'll have a look at that in a moment.
And then I've imported the mysql module,
everything from there just to make my application easier.
So for example, the conn structure
for connection and also prelude like so.
And then also I've imported,
I've got a structure type called Employee.
So our model class is going to be representing one employee.
I'm gonna be storing employees in the database.
So I've defined the employee in types.rs.
So should we have a look at that?
Okay, so here is my employee structure.
In terms of data, it's quite straightforward.
Oh, and also for the sake of simplicity,
I wanted to make this example as simple as I could.
I've actually got public fields.
In reality, you'd have private fields and public functions
but I could do that.
I just wanted to focus on the database access.
So this employee structure,
it's kind of like an entity class in other APIs.
It kind of maps to the table structure.
The employee_id, a 32-bit integer.
An employee has a name and a salary
and a region where they work, like London.
And in terms of implementation, I've got a function,
which will basically create a new instance.
It's like a factory function.
So it's an associated function
where I'd say Employee new, I'd pass in the id.
So if I read a row of data from the database,
I can pass that data into here.
And what it'll read to me back is a new Employee object
containing that data.
So that's what I'm going to do.
I'm gonna fetch data a row at a time from the database.
Each row, I'm going to create an Employee instance from it.
And also, I've implemented the Display trait.
Remember the Display trait?
It allows you to output an Employee object.
You could say something like this, and println macro.
Oh, my application seems
to have reached a point of conclusion.
I'll come back to look at those warnings in a minute.
I could output, using the default formatter,
I could output an Employee object like that.
And the default formatter, as we know, it relies
on the Display trait and in particular, the format function.
So that would be an employee and it can be formatted
to return back basically a string.
And when I write using the formatter,
the string that it returns, which will be displayed
in my application will just display the employee's ID, name,
salary and region.
So I'd call that a model class.
Like I said, similar to an entity class if you're familiar
with Hibernate or JPA or Entity Framework in .NET.
Similar kind of idea.
There's no direct mapping from the table structure
to my employee data.
It's just by convenience, I've decided
to make it a similar kind of data structure.
So let's just have a quick look at these warnings down here.
When I ran the application, I spent an eternity
pulling down the mysql dependency plus the sub dependencies
and eventually, it finished
and then it's given me a bunch of warnings.
Hardly surprising really because I've got a whole bunch
of functions defined everywhere that I'm not actually using.
The reason for those warnings, of course,
is because in my main, I commented out all the code.
So no wonder it's complaining about unused things
because I'm not actually calling any code anywhere.
So if i just uncomment that code,
then I'm not gonna run the application again just yet
but it will now be in a fit state to run in the next demo.