Skip to content
Related Articles

Related Articles

Rust – Hello World Program

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 12 Mar, 2021

Every programmer starts their programming journey with a simple “Hello World!” program. In this article, we will write our first “Hello World!” Rust program. If you have not yet installed Rust on your system, please go through the link and install it.

In this article we will be working on the following topics:

  • Creating a Project Directory for the Hello World program
  • Writing the Hello World Rust program
  • Running the program
  • Understanding the working of the program

Creating a project directory

We will start by making a project directory to store Rust code. We can store our Rust code wherever we want. I will create my project directory on Desktop for simplicity.

Open a terminal and enter the following commands to make a projects directory and a directory for the “Hello, world!” project within the projects directory.

> cd Desktop
> mkdir gfg_project
> cd gfg_project
> mkdir hello_world

We have successfully created our directories.

Writing a Rust program

Next, Under the hello_world directory, make a new source file and call it Rust files always end with the .rs extension. If you’re using more than one word in your filename, use an underscore to separate them. For example, use rather than

Here we will also be making use of the println! macro. A macro system in Rust is used for metaprogramming. These look similar to functions except that they end with a bang “!”.

Now open the file and write the following code :


// Program to print Hello, world!
fn main() {
    println!("Hello, world!");

Now save the file and come back to the command prompt.

Running the program

On the terminal, you must be in the directory where you have stored the file you just created. 

Now enter the following command to run your code.

> rustc
> .\gfg.exe

Output :

Hello, world!

Congratulation! We have successfully written our first Rust program.

Working of the program

Now we will discuss the working of our program. 

fn main() {


This is the first line of our code. Here we have declared the main function, and it is always the first code that runs in every executable Rust program. Inside main() function we have :

 println!("Hello, world!");

It prints the text to the screen.

To execute our program, we must have to compile our code using rustc command followed by a source file like this.

> rustc

After compilation is finished, it generates an executable file. Now we can run this .exe file to get our result.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!