Welcome beginner to this bravefull journey. You’ve started learning Rust and as any other programming language you have to start with hello world!
Simple way
You’ll have to create a file. We will called it hello_world.rs
:
|
|
Next we will write our first Rust script. To run a Rust script, everything must be inside the main function:
|
|
And that’s it, we’ve created our first Rust script. To run it we will have to compile it first and then execute it. To do so, run:
|
|
The right way
We, rustians, create packages with cargo. Cargo is a powerful tool you’ll use compile, build, check and more stuff that will make your life easier. First, we create a new Rust project:
|
|
This will create a new folder called hello_cargo. Inside we will found several files and folder, but we will just ignore it this time.
Next we will go to src/
directory. There we should see a script called main.rs
. You should see that this script already has a hello world script:
|
|
We will run the following command to build and run our code:
|
|