Table of Contents📚
Hello world👋
Declaring a function
We will start this course by creating an hello world program with the rust 🦀 programming language.
First, we create a new folder 📂 hello-world
and a file 📄 main.rs
inside it.
ℹ️
.rs
is the extension for rust files.
We can now open the file 📄 main.rs
in our IDE, as well as a terminal of your choice, and start writing our program.
We declare a function called main
because all rust programs begin with a function main
.
// 📄 main.rs
fn main() {
}
ℹ️ the
fn
keyword is used to declare a function.
ℹ️ the lines starting with
//
are comments, they are ignored when executing the program.
Printing a message💬
In the function, we will call the println!
macro to print a message to the console. And we pass the string Hello, world!
as an argument.
// 📄 main.rs
fn main() {
println!("Hello, world 👋");
}
ℹ️ All instructions end with a semicolon
;
Compiling and running a program🏃
Now that we have coded our program, we will compile it and run it.
to compile the program, we can use the rustc
command.
$ rustc main.rs
ℹ️
$
: command
Running this command created an executable file called 📄 main
in the current directory that we can run by typing ./main
.
$ ./main
> Hello, world 👋
ℹ️
>
: Output of the command