Rust Lang

How to Create a Hello World Program in Rust

There is one building block that all programmers share: a Hello World program. It is a simple but standard program that is specifically designed to be an introduction to a new language. In any language, the Hello World program, introduces the new users to the syntax of the language.

In this article, we will explore how to create our first Hello World program and discuss the anatomy of the Hello World program in Rust.

NOTE: This article assumes that you have installed the Rust compiler and all the required tools. We also assume command-line knowledge.

Project Setup

Although the Rust compiler does not care where your code is within the Filesystem, it is good to organize your code for readability and maintenance.

To follow along with our Rust series, create a directory called rust-workspace as shown below:

$ mkdir ~/rust-workspace
$ cd ~/rust-workspace

Next, create a directory to hold the Hello World program as follows:

$ mkdir HelloWorld
$ cd HelloWorld

Rust Hello World Program

Once we have our project setup, we can begin writing some code. Start by creating a main file ending with .rs extension. This indicates that the file contains Rust source code. For example:

$ touch

Feel free to name your source file any way you see fit. Ensure the filename does not contain spaces as the compiler may cause errors when compiling the code. You can use camel casing or underscores to combine multiple names.

Next, open the file with your text editor and add the code as shown below:

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

Save the file and close the editor.

Open your terminal window and navigate to the HelloWorld directory we created earlier to run the program.

Compile the program using the following rustc command:

$ rustc

The previous command should create an executable program with a similar name as the source file.

To run it, use the following command:

$ ./main  # linux
$ ./main.exe # Windows

Once you run the program, see the text “Hello, World!” printed to your terminal window:

$ ./main
Hello, world!

Rust Hello World Breakdown

Let us go over the details of a Hello World program we just wrote.

The first line of our program is provided below:

fn main()

The previous syntax defines a function in Rust. In our case, we create a main function, which is a special function in Rust. It serves as the starting point for a Rust program where the program’s execution starts.

The main function does not have any parameters nor does it have a return value. If it declares a function with parameters in the Rust language, include them inside the pair of parentheses.

The next part is the function body, which is denoted by the opening and closing curly braces:

   // function body

Although you can have the opening curly braces on a new line, it is recommended to have the opening curly brace on the same line as the function name, but separated by a space.

Within the main function body, we have our line of code as follows:

println!("Hello, world!");

This acts as the heart of the program, and it handles the Hello World program. Pay attention to the println! Keyword.

In Rust, the println! is known as a macro which allows for metaprogramming in Rust. The value of the println! is the string “Hello, world!”

Finally, we have the semi-colon that shows the end of the expression. We should terminate most expressions in Rust with a semi-colon.


Using this guide, we explored the anatomy of a Hello World program and how each part of a Rust program is composed. Keep in mind that a Hello Word program only represents the bare essentials of any program. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list