Hello Rust!

Hello world on Rust

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:

1
touch hello_world.rs

Next we will write our first Rust script. To run a Rust script, everything must be inside the main function:

1
2
3
fn main() {
    println!("Hello world");
}

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:

1
2
rustc hello_world.rs \ # This will generate a binary called hello_world
./hello_world             # This will run the binary

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:

1
cargo new hello_cargo --bin

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:

1
2
3
fn main() {
    println!("Hello, world!");
}

We will run the following command to build and run our code:

1
2
3
cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `debug/hello_cargo`
Theme Stack designed by Jimmy