- Okay, so in this section we're going to see how to build
and run our first Rust application.
Here's the code we're going to compile.
It's hello world.rs simple main function.
It calls the print ln macro,
prints hello world on the console.
What could be easier than that?
Just a reminder, if you want to do these demos for yourself
you can go to the Rust dev demo folder.
Lesson 01 getting started.
That's the folder for the demos for this first lesson.
So let's just confirm that.
So here we are.
I'm in the Rust dev lesson 01 getting started folder.
Here's the simple application, hello world.rs.
Later on in this lesson we're also going to see
how to create, build and run an application using Cargo.
We'll see that later
but we'll just focus on the simple example for now.
Let me just open that up in the code editor.
So here we are.
I'm using Visual Studio Code.
As I was saying previously, other editors are available.
Sublime, Eclipse, VIM, anything you fancy really.
If you are going to use Visual Studio Code
there are extensions available too
so that you can actually run the application directly
within the Visual Studio Code environment.
And I was tempted to illustrate that in the video series
but I decided not to
because you might be using a different tool
and I didn't want to kind of mandate
or make you feel like you had to use Visual Studio Code.
But if you are interested, have a look
at the extensions and download the extensions
for Rust and it'll definitely help your experience.
What I'm going to do is I'm just gonna compile
the application from a normal command window.
So let's see how to do this.
Just open a command window,
go to the folder where your code is
and then run the Rust compiler rustc.
Give it the name of your application.
Quite straightforward, really.
What it'll generate
obviously the executable, your application that you can run.
If you're on Windows, it also generates a PDB file.
This is a like a programming debug information
so that it helps you single step through the application.
It contains symbolic debug symbols to help you
with your kind of diagnostic debug sessions.
Okay, well let me do that.
I'm running Windows
so let's run the Rust compiler on my machine.
Okay, so here we are.
I've opened the folder.
I've opened the command window in the right folder.
So let me just run the Rust compiler, hello_world.rs
and it will generate, let's see what it's generated.
The executable application, which is great
and the the PDB file as well.
So that's great.
So I could run the application.
Now I'll just show you how, look, if I had an error message.
So if we go back in the code
let's say I was a Python developer and in Python
instead of saying fn, you say def.
So maybe I was thinking about Python when I wrote my code.
Okay and maybe I really was thinking about Python
and I had a colon instead
of curly brackets because that's how it would be in Python.
So lots of errors here.
I wonder what the compiler would make of that.
Let's try it again.
Okay, so there's an error online one.
Oh, that was a bad start.
It's expecting some kind of simple
the main function or this is the scope operator.
We'll have a look at that later.
So it's clearly got confused, but then look at this
it gives me helpful error messages, shock, horror.
Maybe you should write fn instead
of def to declare a function.
Well, yes, maybe I should.
So let's fix that, save it.
Go back into command window and recompile.
I wonder what we're gonna get this time.
Oh dear, oh dear.
It's confused about the the colon.
It was expecting this colon to be a curly bracket perhaps
or maybe we'll have a look at the syntax later on.
The arrow syntax here is how you can specify the return type
from a function you could select arrow int, for example
if it was meant to return an integer.
So it's got horribly confused at this point.
Usually when you get multiple errors, you ignore everything
apart from the first one.
Fix that and then hope that it fixes the others.
So I realize that this is a Rust program
not a Python program.
I save the file and I recompile.
And we're back in the room.
So the application is ready to run.
To run it, let's see how to run it.
So to run the application
if you are on Linux or Mac, just run it like so.
And if you run it on Windows like I am,
then you just run it like that
with a back slash instead of a forward slash.
So .\hello world.
Of course I could say hello world.xe on Windows
but I don't need to.
And it was well worth the wait.
So hello world.
Marvelous.
Just a quick reminder, I talked about this in the
one of the earlier sessions, the Rust Playground,
an online editor where you can play with Rust syntax
without having to actually install it on your machine.
Most languages these days have some kind of playground
an online editor for you to have a play.
So for Rust, this is the Rust playground window.
Let's have a quick look at that.
So here it is.
This is the code that was actually in the Rust playground
when I opened it.
And it looks fine to me, so I can run it.
In fact, I'll miss off a curly bracket.
I'll have a deliberate error here and I'll try to run it.
So the compiler runs online
and the error message gets report here.
It's the same compiler, but in an online kind of experience
and it's complaining about an unclosed delimiter.
The curly bracket that started here is not closed here.
Oh, I see what I've done wrong.
I forgot my curly bracket.
Let's try that again.
The compiler when you do online can be a little bit slow
because it's kind of like a remote experience.
So you have to be a little bit patient
when you're running it.
So I'm just gonna wait for it to be compiled
and it has compiled successfully.
And here's the output from the application.
Okay, so I'm not going to be using the playground a lot
during the demos
because the code we've got is gonna be more significant
and we'll kind of do it properly.
But if you haven't yet got the compiler installed,
you can always try examples here just as a starting point.