Rust Hello World


Rust Programming


Rust Programming



In this tutorial, you will learn to write a "Hello World" program in Rust.

Procedure to write first program.


  • Step 1: Create a RustApp folder and navigate to that folder on terminal. (Using File manager)
  • Step 2: Create folder using terminal.
C:\Users\Admin>mkdir RustApp
C:\Users\Admin>cd RustApp
C:\Users\Admin\RustApp>{codeBox}
  • Step 3: To create a Rust file, execute the following command
C:\Users\Admin\RustApp>notepad Hello.rs{codeBox}

Rust program files have an extension .rs. The above command creates an empty file Hello.rs and opens it in Notepad. Add the code given below to this file. 

Input

fn main() {
    println!("Hello, World!");
}{codeBox}

Compile the Hello.rs file using rustc.

C:\Users\Admin\RustApp>rustc Hello.rs{codeBox}

Output

Hello, World!{codeBox}

Explanation

The above program defines a function main fn main(). The fn keyword is used to define a function. The main() is a predefined function that acts as an entry point to the program. println! is a predefined macro in Rust. It is used to print a string (here Hello) to the console. Macro calls are always marked with an exclamation mark – !.

What is a macro?


Rust provides a powerful macro system that allows meta-programming. As you have seen in the previous example, macros look like functions, except that their name ends with a bang(!), but instead of generating a function call, macros are expanded into source code that gets compiled with the rest of the program. Therefore, they provide more runtime features to a program unlike functions. Macros are an extended version of functions.


Credits & Copyrights©: Javatpoints & programiz

Post a Comment