DEV Community

Cover image for Write your first Hello World! program in Rust
Anshuman Sathua
Anshuman Sathua

Posted on • Edited on

Write your first Hello World! program in Rust

Why Rust:

Imagine crafting a rocket with Lego, where the pieces not only magically stay together but also soar through the skies at astonishing speeds without a hint of a crash. That's Rust in a nutshell: a programming language that's powerful, safe, and remarkably enjoyable to work with.

A Word of Caution:

  • Young Ecosystem: With fewer tools compared to a seasoned language, consider yourself a pioneer forging new paths!
  • Learning Curve: It's more of a mountain than a hill, but the view from the top is breathtaking! ⛰️
  • Not for Everyone: If your goal is a quick website, stick to simpler tools for now.

Alright, enough talk—let's dive in.

Installation:

To embark on our Rust journey, we'll use rustup, a command-line tool for managing Rust versions and associated tools.

  • For Linux and macOS users, follow these steps.

  • Windows users can click here. The installation process is straightforward, but if you encounter any issues, drop a comment below, and I'll do my best to assist.

Writing and Running a Rust Program

Now that Rust is at our fingertips, let's write our inaugural Rust program. Open a terminal and enter the following commands to create a projects directory and a subdirectory for the “Hello, world!” project:

For Windows CMD:

> mkdir "%USERPROFILE%\projects"
> cd /d "%USERPROFILE%\projects"
> mkdir hello_world
> cd hello_world
Enter fullscreen mode Exit fullscreen mode

For Linux, macOS and PowerShell on Windows:

$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world
Enter fullscreen mode Exit fullscreen mode

Next, create a new source file named main.rs. Rust files always end with the .rs extension. Add the following code to main.rs:

fn main() {
    println!("Hello, world!");
}
Enter fullscreen mode Exit fullscreen mode

Save the file and return to your terminal in the same directory. Compile and run the file using the following commands:

For Windows:

> rustc main.rs
> .\main.exe
Hello, world!
Enter fullscreen mode Exit fullscreen mode

On Linux and macOS, use ./main instead of .\main.exe. The terminal should display the string "Hello, world!"

Understanding the Basics: Anatomy of a Rust Program

Let's dissect the program. Initially, we have:

fn main() {

}
Enter fullscreen mode Exit fullscreen mode

The first line declares a function named main with no parameters and no return value. The function body is enclosed in curly braces {}. The main function is special—it's always the first code that runs in every executable Rust program.

Inside the body of the main function, you'll find:

    println!("Hello, world!");
Enter fullscreen mode Exit fullscreen mode

This line prints the string to the terminal.

A couple of things to note: end a line with a semicolon (;) to indicate the completion of the expression. Additionally, println! calls a Rust macro. At this point, let's consider macros a mystery to unravel in our future, more advanced explorations. Remember, if you're writing println!, you're calling a Rust macro, not a regular function. For regular functions, use println without the !.

Compiling and Running

To run a Rust program, first, compile it using the Rust compiler with the rustc command:

$ rustc main.rs
Enter fullscreen mode Exit fullscreen mode

If you're familiar with languages like C or C++, this is similar to using the gcc compiler. For those accustomed to dynamic languages like Ruby, Python, or JavaScript, the concept of compiling and running as separate steps might be new. Rust is an ahead-of-time compiled language, generating a binary executable file. It doesn't matter where your code lives, as long as you have the executable file, you can run the program even without having Rust installed—something not possible with dynamic languages like Ruby, Python, or JavaScript.

After compilation, on Linux and macOS, you can see the executable file:

$ ls
main  main.rs
Enter fullscreen mode Exit fullscreen mode

On Windows, it might look like this:

> dir /file names
main.exe
main.pdb
main.rs
Enter fullscreen mode Exit fullscreen mode

This displays the source code file with the .rs extension, the executable file (main.exe on Windows, main on other platforms), and, when using Windows, a file containing debugging information with the .pdb extension. To run the program, use:

$ ./main  or .\main.exe on Windows
Enter fullscreen mode Exit fullscreen mode

This should print Hello, world! to your terminal. Congrats!🎉 You've written your first Rust program. Keep an eye out for future updates, we'll explore more about Rust in the future. Thanks for reading, I hope it was worth your time. Have a fantastic day!

Top comments (0)