Hello World is the first program to learn any 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 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 any text editor and create a file called hellowrold.rc, Here rc is an extension used for creating a rust program.

helloworld.rc:

In 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 console

// main function executes when 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 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 console. In Rust, Strings are enclosed in double or single quotation marks.

The next step is to compile and run the program.

Open 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 thehelloworld 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 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 read the command line arguments and prints them to 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. 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 and 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