Hello World in Rust
Introduction to Rust
Rust is a systems programming language that emphasizes safety, speed, and concurrency. It is designed to be a reliable and efficient language for building software, with a strong focus on memory safety without using a garbage collector.
In this tutorial, we will walk you through the steps to create a simple "Hello, World!" program in Rust, which is typically the first program that beginners write in any programming language.
Setting Up Rust
Before we can write our "Hello, World!" program, we need to install Rust on our machine. The recommended way to install Rust is through rustup, which is an installer for the Rust programming language.
Installation Steps
- Open your terminal or command prompt.
- Run the following command to download and install rustup:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- Follow the on-screen instructions to complete the installation.
- Once installed, you can verify the installation by running:
rustc --version
Creating Your First Rust Program
Now that we have Rust installed, we can create our first program. Follow the steps below to write and run your "Hello, World!" program.
Step 1: Create a New File
Use your favorite text editor to create a new file named main.rs
. This file will contain our Rust code.
Step 2: Write the Code
Open main.rs
and add the following code:
fn main() {
println!("Hello, world!");
}
Step 3: Save the File
After writing the code, save the file to ensure your changes are not lost.
Compiling and Running the Program
With the code written and saved, it's time to compile and run the program. Back in your terminal, navigate to the directory where main.rs
is located and run the following command:
This command compiles the Rust code in main.rs
and generates an executable file. On Windows, this file will be named main.exe
, while on Linux and macOS, it will simply be named main
.
Now, run the executable with the following command:
Hello, world!
Congratulations! You have successfully written and run your first Rust program.
Conclusion
In this tutorial, we learned how to set up Rust, create a simple "Hello, World!" program, and run it. This is just the beginning of your journey with Rust, which has many powerful features to explore, including ownership, lifetimes, and concurrency.
Happy coding, and welcome to the world of Rust!