- [Instructor] Okay, so in this section,
we are going to see how to use Cargo
to create a new Rust application.
Cargo is part of the Rust toolset.
In practice, you will always use Cargo
whenever you are creating a Rust application
or building it, or running it, okay?
So, up until now I've been writing a handcrafted Rust file
and compiling it using the Rust compiler.
You can do that,
but it's better to use Cargo for everything.
So, it's the kind of one-stop shop
for everything that you want to do in Rust.
It will generate an application initially,
it'll give you a consistent kind of directory structure
and it nudges you towards best practice, okay?
So it's basically the tool you should always be using
whenever you are building,
or creating, or running a Rust application.
So, like I said,
it's included as part of the standard Rust toolset.
So if you've installed the Rust compiler,
you've also installed Cargo already.
This is how you can use Cargo
to generate the new application.
You say "cargo new"
and then you give it the name of the application.
Rust prefers your application to use lowercase
and underscores.
This is called snake case and this is what Rust prefers.
So I'm going to do that.
I've already actually done this in the demo package,
let me show you.
Here's the demo folder for the current package
for the current part of the demo.
rustdev\lesson01_getting_started.
And you'll notice
that I've already got hello_world_via_cargo.
Okay, so we could look at that
or we could generate a new application
which is what I'm going to do instead.
cargo new,
and hello_world_via_cargo_2.
That's going to be my new application.
Okay, so that was quick.
And what you'll find,
it's generated a new folder that contains that application.
So I now have a new folder,
hello_world_via_cargo_2.
So, I'm gonna change into that directory,
and let's see what we've got generated.
So, it generates,
well, it actually generates,
you can't see it here,
but it's generated a Git repository folder
so that you can upload your application
to a Git repository, or Bitbucket,
and a .gitignore file
specifying what files not to upload to Git.
All the source code is in the source folder.
And there's also a package file called Cargo.toml
which we're gonna have a look at in a moment.
Okay, so just a confirmation.
This is what it actually looks like.
There is a Git folder, which is the Git repository,
the source folder, which contains our code
a .gitignore file,
and then a toml file
which contains basically information about our application,
what dependencies it has,
what version number it is and suchlike.
Okay, so this is the packaging file,
containing metadata about our package
and the list of any other libraries
or any other dependencies our application needs.
The Rust library is fairly complete
but there were third-party packages,
third-party libraries,
that you might also need to access.
For example, if you're talking to a database,
you'll need to download some app packages
that have database capabilities.
So we'll have a look at how to add additional dependencies
into your application later.
At the moment, this is what the toml file looks like.
Whatever the name of the package is, the application,
so for us it would be hello_world_via_cargo_2,
a version number, with kind of like a release number
and a major version and a minor version,
that's up to you to manage.
That's your number.
You can change this as you like.
And then the addition of the Rust compiler that you're using
as generated by the Cargo tool.
So that's information about your package, your application.
And then this section here,
dependencies is empty at the moment,
but later on we'll be adding entries in here
which give us access to,
for example there's a library called Chrono
for time manipulation.
There's a library called MySQL to access the MySQL database
and so on.
Okay, so later on in the video series,
we'll be adding items underneath here
to download libraries that our application needs.
Okay, so I'm gonna open my application,
hello_world_via_cargo_2.
I'm gonna open this one in Visual Studio Code.
And here it is.
You see there's the toml file.
And, as described,
and here's the source file.
And in the source file,
we just have a single main.rs.
Okay, as we would expect.
So, we're kind of at the point now
where we've generated the application using Cargo,
it has the source code,
obviously we can add other source files
in here if we wanted to.
And it has the toml file,
which we could edit if we wanted to,
and then we could use Cargo
to build the application and to run the application.
Okay, and that's what we're going to see
in the next section.