Getting Started with “Hello, World! "

It’s universal truth everybody starts their journey with the hello world program.

create hello.rs file with the following content :

fn main() {
    println!("Hello World!");
}
  • In Rust, functions are declared using the fn keyword followed by the function’s name. For instance, the main function is the entry point of a Rust program.

  • println! is a macro, not a function, and is used for printing text to the standard output (STDOUT). The exclamation mark distinguishes it as a macro. The program’s code, including the main function, is enclosed within curly braces {}.

  • In Rust, execution begins with the main function. If a function has parameters, they are specified within parentheses () after the function’s name. The main function in the standard format, with () and no parameters, indicates that it does not accept any arguments.

  • To execute a Rust program, you need to compile it first. This is done using the Rust compiler, rustc. After compilation, an executable is generated which can be run to execute the program

  $ rustc hello.rs
  

on macOS, you can use the file command to see what kind of file this is

  rustlabs file hello
hello: Mach-O 64-bit executable arm64
  

you can execute your program like this on mac

  rustlabs ./hello
hello,world!
  

The dot(.) indicates the current directory and the slash(/) is the path separator. The path separator is a forward slash(/) on macOS and Linux and a backslash() on Windows. you can execute your program like this if your using windows

  rustlabs ./hello.exe
hello,world!
  

🍾 cheers!

Knowledge check

--- primaryColor: steelblue secondaryColor: '#e8e8e8' textColor: black shuffleQuestions: false shuffleAnswers: true locale: en --- #### What is the keyword for declaring a function? - [ ] fun - [ ] function - [ ] func - [x] fn #### What is the output of the following code? ```rust fn main() { println!("Hello World!") println!("Hello"); } ``` - [ ] Hello World! \n Hello - [X] error

Organizing a Rust Project :

for any project, we all write multiple source files and execute them as a single binary. let’s remove our existing hello binary rm hello

let’s create a parent directory

  
$ mkdir -p hello/src
  

now move the hello.rssource file into hello/src using the mv command

  $ mv hello 
$ rustc src/hello.rs
  

check the content of the directory using tree command :

  ➜  rustlabs tree
.
└── hello
    └── src
        └── hello.rs
  

What is a macro?

A macro is an expression that has an exclamation mark (!) before the parenthesis () , i.e.,

macro_name ! ( );

What are macros used for?

Macros in Rust are powerful tools for metaprogramming, which involves writing code that generates other code. Unlike functions in languages such as C or C++, macros don’t result in function calls. Instead, they expand into source code that is then compiled along with the rest of the program. This approach allows for additional runtime capabilitie

Types of Macros

An example of a built-in macro in Rust is println!, which is used for printing output. Rust also allows users to define their own custom macros.

While this overview provides a basic understanding of macros, more in-depth information on this topic is available in advanced Rust programming resources.

Last updated 25 Jan 2024, 05:11 +0530 . history