Rust Tutorial – Part 1 – Hello World

[This post is part of the series on Rust programming tutorials that I’m writing for enjoyment. All the tutorial links are here.]

Every programming language must begin with printing “Hello, world!” to the screen. It’s not mandatory, but it’s a tradition honored by great programmers through the ages; and who am I to break that tradition?!

So, having installed Rust on your machine, let’s move on to writing our very first Rust program. Create a directory where you’ll be storing the Rust files (here’s how I organize code used for learning); let’s call this directory `rusty-learning`. Inside this directory, we’re going to create a new file to start our journey. Use your favorite text editor (ed, nano, vim, emacs, Sublime, Notepad++, Comodo, Spyder, whatever . . .) and create a new file called The rs extension hints that it’s a Rust source file. (Why add the unnecessary 01 at the beginning? It’s part of the scheme I follow, and is explained in the article linked above.)

Let’s add the following code to this file:

fn main() {
    println!("Hello, world!");

(I’m sorry for the plain-vanilla look. I couldn’t find a WordPress plugin that supports Rust syntax highlighting out of the box. I’ll keep looking, though.)

We’ll get to the explanation in a minute; for now, let’s run the code and see what happens:

$ rustc

The rustc command invokes the Rust compiler (much like the gcc command if you’ve done any C programming) and asks it to compile the file to a binary executable file. If all goes well (hopefully there are no typos), you’ll see . . . nothing.

Yes, that’s right. If the command prompt quietly returns, it means Rust was able to compile your file successfully. How can you be sure? Simply list all the files in the directory and you’ll see this:

$ ls . 01_hello_world

Look again. There’s a second file now: named 01_hello_world, and it has no extension. That’s because it’s a binary file and can be run straightaway:

$ ./01_hello_world
Hello, World!

Three cheers! We’ve just run our first Rust program successfully!

I admit the output isn’t very spectacular, but hey, the journey of a thousand miles — okay, okay, don’t hit me. I know you don’t like decrepit quotes. 🙂

On to how the program works.

I believe that for experienced programmers, the program is a cakewalk. But just in case you’re from a scripting language background, here’s some introduction.

In compiled languages, there has to be an entry point for the compiler, from where it starts understanding your code, linking it and converting it into one big executable. Conventionally, this entry point has been a function called main, which is what we see here. Without main, the compiler will think that you’re trying to create a library file (a set of reusable code files) and will not produce an executable.

So, once the compiler sees main()it knows it’s time to start building the program. It then encounters the next line, which calls the println marco (notice the ‘!’ at the end of the function name? That shows it’s a macro). Macros are different from functions, and are part of an advanced topic called Metaprogramming. Although we’ll cover Metaprogramming much later, for now it’s enough to understand that macros allow us to get underneath the skin of the language and do things that its syntax wouldn’t normally allow.

So, the println! macro sends the given string to the standard output, which happens to be your terminal in this case. As a result, you see “Hello, World!” printed.

I hope you enjoyed this tutorial. If you have any comments or rants, feel free to vent out below. 🙂

Go to next tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *