[No audio]
Hello and welcome back.
In this tutorial we will be looking at some
basic stuff related to compiling and executing programs.
I will make a new folder for saving all of my files.
[No audio]
Let me add a new file of
Example1.rs to this folder.
All the Rust code files needs
to have an extension of .rs.
Let us add a program similar to the one
that we looked at in the previous tutorial.
We will start by adding the main function.
You may notice that as I write the code, there
are some suggestions that are coming along and that is
because of the extensions for us that we have installed.
The main function is like an entry point into the code.
When the compiler compiles our program, it will
look for the main function and will start
executing the code that is contained inside it.
Next, when I put the starting parentheses,
the ending parentheses are automatically being generated.
This is quite handy and cool because
it eliminates the chances of forgetting it.
Next, I will add a print statement and we'll add the text
of Hello World to it to be printed on the terminal.
Again, you may have noticed that as I write
there are some suggestions and the closing brackets and
the quotes are being auto generated for us.
Okay, this program is now complete.
This small bubble is an indication that
the program has not been saved yet.
The editor will not autosave our program and
you will need to do it manually.
It is a common mistake at the beginner level that
we execute our code without saving the latest changes.
Always make sure that you save the latest version
and then execute your program for correct results.
To compile the program, we will
write commands in the terminal.
The terminal may be made visible from the view menu.
[No audio]
The rustc command, followed by the name of
the file is used to compile the code file.
[No audio]
It will take a few seconds and if
everything is okay, it will return the control
to the terminal without any additional messages.
You may also note that it has generated the
executable file corresponding to our file, which we can
now use to see the program output.
This can be done by writing the command on the
terminal ./Example1, and it will run the application and
will have the appropriate output on the terminal.
This is useful when your program is fairly
simplified and contains only a single file.
However, in real world scenarios, your programs
may tend to be complex, having dependencies
and more than one file.
In that case, you should be
ideally using the Cargo utility.
As pointed out in the previous tutorial, Cargo
is the package manager to install packages and
to track the dependencies and other related stuff.
It is therefore more efficient and effective to use
Cargo for compiling and building our programs in Rust.
Before explaining how to compile and execute the
program using Cargo, I will first delete these
two files that we have just created.
We will first initialize a project using Cargo
using the command of cargo new, and then
the name of the directory or folder.
[No audio]
This will create a new
project in the specified directory.
You may have noticed that we have
a new folder called learning_rust.
When I open this, you may see some files inside it.
One of these files is the Cargo.toml file
which contains information like the name, the version, and
is very similar to the file like pip file
for Python, and JSON for Node.
It contains all the necessary information about our
application and any dependencies that you may have.
By the way, we will cover the dependencies later
on in the course, but for now, just keep
in mind that we will need some information here
when we use other people code and functions.
You may also note a .gitignore file which contains
the location of the folder where our additional files will
go when our code gets compiled, which may not be
directly useful for us, but the compiler generates them.
Finally, we have a source folder, that is src, which
contains all the source files with the .rs extensions.
Inside the source files there is one
rs file which is kind of autogenerated
when we first initialize a project. It contains
the same code that we just saw earlier.
To compile the code in the .rs file, we
will use the command of cargo run.
[No audio]
It will not only compile but also execute the program
and will give us the necessary outputs if any.
Please note that it compiled into the
target folder and then into the Debug.
And right here is the executable file with
the name of learning_rust.exe.
This means that I can run using the
command of ./target/debug/learning_rust.
If you just want to build but not run,
then you will use the command of cargo build.
[No audio]
In this case, it will not
be printing anything on the screen.
If you are interested in building it for production, then
you will use the command of cargo build --release.
[No audio]
It will take some time and afterwards
it will have the message of, Finished
release optimization, which is an indication that
it is now being optimized for production.
Inside the target folder and then inside the
debug there is another folder called release, which
is being created where the optimized version of
the executable has been created.
This executable can now be used for deployment
in a real world scenario. The optimizations
make your Rust code run faster.
However, to optimize the code, it
may take more time to compile.
To summarize, there are two scenarios.
In the first scenario we are in the stage of
development where we would like to rebuild quickly and often,
since we are in the process of writing the code
and we have not yet finalized the code.
In the second scenario, we are done with the code,
and now we would like to finalize the program
that we will be handing over to the user.
So in this case we won't be rebuilding repeatedly.
The objective will be that the
code runs as fast as possible.
In this scenario we will use build using the
release options which will do the optimizations for us.
This is basically all about
the compilation of your programs.
For majority part of the course we will be using the
cargo run and for writing codes we will be generally using
the main function and will write the codes in the main.rs file
which is residing inside the src folder.
That is it for this particular segment of the course.
See you again, and until next tutorial,
enjoy rust programming.
[No audio]