Rust is a powerful systems programming language that focuses on safety, performance, and concurrency. Whether you are a seasoned developer or just starting your programming journey, learning Rust can be a rewarding experience. In this blog post, we will guide you through the installation process and help you write your first Rust program - the classic "Hello, World!" program.
Before we dive into writing Rust code, let's get Rust installed on your system. Follow these simple steps:
Visit the Official Rust Website: Open your web browser and go to the official Rust website: https://www.rust-lang.org/tools/install
Select the Correct Download: Rust supports various platforms, including Windows, macOS, and Linux. the website should automatically detect your operating system. If it doesn't, select your platform from the list provided.
Run the Installer: Once the download is complete, run the installer. The installation process will vary depending on your operating system. Follow the on-screen instructions to complete the installation.
Verify the Installation: To ensure Rust is properly installed, open a terminal or command prompt and type the following command.
If the installation was successful, you should see the installed Rust version displayed on the screen.
Congratulations! You've successfully installed Rust on your system. Now, let's move on to writing our first Rust program.
Hello, World! In Rust
The "Hello, World!" program is a simple tradition in the programming world. It's a basic program that prints the words "Hello, World!" to the console. In Rust, it's just as straightforward. Follow these steps to write and run your first Rust program:
Create a New Project: Open your favorite text editor or IDE (mine is VSCODE) and create a new file. Save it with a .rs extension, which indicates it's a Rust source code file. For example, name it hello_world.rs.
Write the Code: Open hello_world.rs and type the following code:
In Rust, the
main function is the entry point of the program. The
println! macro is used to print text to the console.
we will discuss more
macros in an upcoming lesson.
Compile the Program: Open your terminal or command prompt and navigate to the directory where you saved hello_world.rs. Use the following command to compile the Rust code:
This will generate an executable file named hello_world in the same directory.
Run the Program: To run the compiled program, simply type its name in the terminal and press Enter:
If everything went well, you should see the output: "Hello, World!" displayed on the screen like the below screenshot.
Congratulations! You've completed your first steps with Rust. You installed Rust on your system and wrote a simple "Hello, World!" program.