Hello World is the first program to learn new programming language.

Following are steps to Write, Compile, and Run the Hello World Program example in Rust.

  • Choose Source Code Editor
  • Create Rust file helloworld.rs
  • Write a program code to print hello world to the console
  • Compile and run the Hello World program To learn Rust programming, You need to have any basic programming experience.

Hello World Rust Program

Open text editor, create a file called helloworld.rc, Here rc is an extension used for creating a rust program.

helloworld.rc:

In the helloworld.rc program, write the below lines of code, and save the file

// HelloWorld.rc Hello World First example program
// It prints the hello world string to the console

// main function executes when a binary compiled file executed
fn main() {
    // Print string to the console
    println!("Hello World Example First Program");
}

The above program contains 2 parts

  • Comments
  • Main Function

Comments: Comments are for a developer to describe about line or block of code. It is ignored by the compiler. main function:

Rust functions always start with the fn keyword and the function name and body are enclosed in {}

fn main() {
    // Print string to the console
    println!("Hello World Example First Program");
}

println! is a rust macro that accepts strings or expressions displayed to the console. In Rust, Strings are enclosed in double or single quotation marks.

The next step is to compile and run the program.

Open the terminal or command line.

rustc helloworld.rc

rustc is an interpreter or compiler that compiles rust code and generates the binary file. It accepts the name of the rust file. This command outputs the HelloWorld binary file in the current directory

A:\work\rust>dir
 Volume in drive A is Work
 Volume Serial Number is C682-8F53

 Directory of A:\work\rust

09-04-2022  16:49    <DIR>          .
09-04-2022  16:41    <DIR>          ..
09-04-2022  16:49           155,136 helloworld.exe
09-04-2022  16:49         1,142,784 helloworld.pdb
09-04-2022  16:47               524 helloworld.rc
               3 File(s)      1,298,444 bytes
               2 Dir(s)  46,152,921,088 bytes free

Next Step, Run or execute the binary file using the file in the terminal

./helloworld

After executing the command, It outputs a string to the console.

Hello World Example First Program

How to read command line output and print to console in Rust?

This program reads the command line arguments and prints them to the console

use std::env;

fn main() {
    println!("Please enter your Name");

    let args: Vec<String> = env::args().collect();

    let name = &args[1];
    let filename = &args[2];

    println!("Name is {}", name);
}

Here is a sequence of steps

  • First print the string to take input from a user. The println! macro is used here
  • Need to take input from a user, env::args().collect(); method used, and waits for the program to read the user keyboard, read the values as Vector
  • Create a variable name and assign the entered value after the enter command
  • Finally print the user name using string print syntax.

After running the above program, the output is

Please enter your Name.
John
Name is John

You can also check Rust Cargo Hello World Program