- In the previous section,
we saw how to create a sample application by using cargo.
It's located here in the rustdev demo folder,
lesson_01_getting_started/hello_world_via_cargo.
So here it is in my file system, rustdev,
lesson_01_getting_started/hello_world_via_cargo.
If I open that file,
if I open that directory in Visual Studio Code, here it is.
Quick reminder, we had the TOML file,
which contains information about our package
or our application plus any dependencies
that our application might have,
we don't have any at the moment.
By the way, TOML stands for Tom's Obvious Minimal Language.
There we go, fun fact.
And in the source folder, of course,
we just had our simple main application like so.
So let's see how to build the application
and how to run it using cargo.
Let's see how to do that.
Okay, so you can either build the application in debug mode
or you can build it in release mode.
If you build it in debug mode, then the compile is fast,
it isn't optimized, so it's not very fast at runtime,
but it's fast at compile time
and it contains debug information.
And so, you know, 9 times out of 10,
you'll be running the application in debug mode
until you've completely debugged it
and then you can build it in release mode later
when it's guaranteed to be working.
So to build the application in debug mode,
open up a command window,
it'll generate in the ./target/debug directory,
that's where your application will end up being placed.
So this is how you do it,
from the command window cargo build.
So I'm going to do that now.
Here's a command window opened in the correct directory
from my application, cargo build.
Okay, and it has generated the application in debug mode.
So if I do a direct research on the target subdirectory,
you'll notice that there's a debug subdirectory,
and in there, in target\debug,
there's the application hello_world_via_cargo plus lots
of other files that are useful for debugging purposes.
So basically all you have to do
right now is to run the hello_world_via_cargo executable
from the target debug folder.
So to do that, it obviously depends
whether you're using Linux, Mac, or Windows
because the slashes go one way or the other way.
I'm running Windows so target, backslash, debug,
backslash, hello_world_via_cargo,
there we go, .\target\debug\hello_world_via_cargo.exe.
Hello, world!
Okay, so that's the application built in debug mode.
If you want to do the same in release mode,
when you're confident that the application is, you know,
debugged and correct and ready to run in production,
then you can build it in release mode.
The build is slower because the compiler's working harder
to optimize your code.
It doesn't contain any debug information
but the application will be faster at runtime,
which is obviously what you want in production.
So it generates the application
in the target release folder, you might have guessed.
This is how you build in release mode,
cargo build --release.
So let me do that now.
Okay, so it has built the release version of the application
optimized for fast execution at runtime
and it is in the target release folder.
Okay, so in the target release folder,
I have my hello_world_via_cargo executable.
So to run it again, depending whether you run in Linux,
or Mac or Windows, go to the target release folder,
run the application.
So here I am, .\target\release slash
hello_world_via_cargo.exe.
Of course it still prints the same application,
but this application is running faster.
Okay, so you can build the application
and then you can run it as separate steps.
Alternatively, you can build
and run the application in one go.
Okay, so you can,
this is probably what you'd wanna do in practice.
In reality, if the application has already been compiled,
then it won't recompile it, it'll just run it.
This is what you do, you say cargo run.
So that will build the application in debug mode by default
if it needs to,
if the application hasn't already been built in debug mode,
if we've changed it, it'll rebuild it and then run it.
Okay, in debug mode
or in release mode, you can say cargo run --release.
Again, it'll build the application in release mode
if it needs to,
and then it run the application in release mode.
So cargo run would build if it needs to in debug mode
and then run it in debug mode.
You can see it's run the debug version
of the application there.
Alternatively, I can say run the application,
build and run, really in release mode,
so now it's gonna build
and run the application in release mode.
So again, in practice, rather than building the application,
and then running it as two separate steps,
in reality you just say cargo run,
it'll build the application if it needs to and then run it.
So that's what we'll be doing from now on.
We'll just say cargo run
for every application for the rest of the video series.
One other thing that's worth mentioning,
there's also a command called cargo check.
So this just does a sanity check on your syntax.
It doesn't attempt to generate an executable,
it's just checking your syntax to make sure
that you haven't missed any semicolons
or curly brackets or you know, any typo like this.
So if you just want check the sanity of your code,
this is faster than building it,
because it doesn't have to generate any executable.
So for example, in here,
if I accidentally on purpose forgot a curly bracket,
and I just wanted to check the syntax,
I can say cargo check.
So let's do that now,
I wonder if my code is correct.
Oh, it isn't, unclosed delimiter,
that delimiter there was not closed here.
Okay, so therefore let me go and fix the error.
I see my error, curly bracket added,
let's do a cargo check again.
And now it has compiled successfully.
It hasn't generated a runnable application,
but at least I know my application is fine.
So that concludes this lesson.
We've seen basic syntax for Rust.
We've also introduced the cargo tool
as a way of generating the application
and then building it and running it.
So for the rest of the video series,
all we need to do now is look
at the syntax for the Rust language itself.