Writing a first program in Rust, traditionally the “Hello, World!” application, is a common starting point when learning a new programming language. The first documented example of this type of program is found in the 1972 book “The C Programming Language”.
you can check the rust Installation procedure
Here’s a step-by-step guide to creating and understanding your first Rust program:
Create a Project Directory
Create a directory to store your Rust code. it doesn’t matter to Rust where your code lives; it’s a good practice to keep your projects organised.
For example, on Linux, macOS, and PowerShell on Windows, you can use the following commands in your terminal to create a projects directory in your home directory and a hello_world directory within it, then navigate into the new directory: Use commands one by one, not used all at once.
$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world
For Windows Command Prompt (CMD), the commands are slightly different: use commands one by one, not used all at once.
> mkdir "%USERPROFILE%\projects"
> cd /d "%USERPROFILE%\projects"
> mkdir hello_world
> cd hello_world
Write the Rust Program
Next, create a new source file inside the hello_world directory and name it main.rs. Rust source files always end with the .rs extension. If you use multiple words in a filename, the convention is to separate them with an underscore, like hello_world.rs.
Open the main.rs file in a text editor and enter the following code:
fn main() {
println!("Hello, world!");
}
Understanding the Program Structure
Let’s break down the components of this simple program:
fn main() { … }
These lines define a function in Rust. The fn keyword is shorthand for “function.”.
- The main function is special because it is always the first code that runs in every executable Rust program.
- The parentheses () following main indicate that this function has no parameters. If there were parameters, they would go inside these parentheses.
- The curly brackets {} wrap the function body. Rust requires curly brackets around all function bodies. These brackets also serve to delimit code blocks.
- The main function by default does not return a value (it returns a “unit”, which is an empty tuple ()). It can optionally return an integer value to the operating system using the Termination trait.
println!(“Hello, world!”);
This line executes the program’s action.
- println! calls a Rust macro. Macros provides extra capabilities for the language. They are a form of metaprogramming where the macro generates another code. At compile time, the result of the macro replaces the macro itself.
- The exclamation point ! after println is crucial; it signifies that you are calling a macro, not a standard function. Macros may not follow the same rules as functions.
- “Hello, world!” is a string literal passed as an argument to the println! macro. Rust strings are UTF-8 encoded.
- The semicolon ; at the end of the line indicates that this expression is finished and the next one is ready to begin. Most lines of Rust code end with a semicolon.
Compile and Run the Program
Rust is a compiled language. It means first compile code into an executable binary before you can run it. This approach is different from dynamic languages where compilation and running might be a single step. Once compiled, the Rust binary can often run on other systems without Rust installed.
To compile the main.rs file, open your terminal in the hello_world directory and use the rustc command:
For Mac os or Linux
$ rustc main.rs
For windows
rustc main.rs
This command invokes the Rust compiler. If compilation is successful, it will produce an executable file in the same directory. The executable will be named main on Linux and macOS and main.exe on Windows.
To run the compiled program: On Linux or macOS:
$ ./main
On Windows CMD or PowerShell:
> .\main.exe
After running the command, you should see the output in your terminal:
Hello, world!
Alternatively, test simple Rust code snippets, including the “Hello, World!” program, using the online Rust playground It provides an editor and environment to compile and run code directly in your web browser.
You can see here Rust program uses