Hello World in Rust

Welcome Back!

How did my suggested exercise fare with you? Were you able to install rust on a different directory?

Last time we were able to install Rust on our machines. For this session we will tackle:

  • The ubiquitous Hello World program
  • Want to run rust programs without compiling anything? Please do tell me!

I would like to introduce you to the Rust Playground

Click here to open a new Rust Playground tab

This is where you can do simple rust programs and let the playground compile it for you inside the Rust playground you can:

  • Run a rust code
  • See the program compiled into ASM, LLVM IR, and MIR.
  • Under Mode, you have Debug and the Release versions of Rust (I usually choose Debug)
  • Under Channel, choices are: Stable, Beta, and Nightly. (I say Nightly if you want to be adventurous).

It’s called Rust Playground for a reason. Play with it. 😀

Now as promised, the ubiquitous Hello World. I am going to show you two(2) ways to run this program.

Solution #1: Rust Playground

Copy-paste the code below to the Rust Playground and click Run

fn main() {
println!("Hello World");
}

Solution #2: Create a rust file in your computer, and then compile it.

  1. Create a file named main.rs.
  2. Edit main.rs and copy-paste the code above.
  3. Go to command prompt.
  4. Type “rustc main.rs” and press Enter.
  5. A file main.exe will be created on the same folder as main.rs.
  6. Execute main.exe.

You will be greeted with a “Hello World”.

Congratulations! You have written a Hello World program in rust.

On my next articles, I will discuss:

  • Ownership
  • Borrowing

No responses yet

Post a comment

Post Your Comment