Getting Started with Rust: Building Your First “Hello, World!” Application

Rust is a modern systems programming language that offers the performance of low-level languages like C and C++, while also providing strong safety guarantees. Its unique blend of safety, concurrency, and performance makes it an excellent choice for building everything from system utilities to large-scale web applications.

In this tutorial, we’ll walk through the process of creating a simple “Hello, World!” application in Rust. By following along, you’ll get a taste of Rust’s syntax and tooling, setting you on the path to becoming a proficient Rust developer.

Setting Up Your Development Environment

Before we dive into coding, let’s make sure you have Rust installed on your system. Rust’s official website provides comprehensive installation instructions for various platforms, including Windows, macOS, and Linux. You can find these instructions to do so here. To verify that you have Rust successfully installed you can run the following command.

rustc --version

This command should display a similar response to the following with exception of the version.

rustc 1.70.0 (90c541806 2023-05-31)

Creating your First Rust Project

Now that your development environment is set up, let’s create a new Rust project. Rust’s package manager, Cargo, makes it easy to initialize new projects and manage dependencies.

In your terminal, navigate to the directory where you want to create your project, and run the following command:

cargo new hello_world

Navigate to the ‘srcdirectory within your newly created project (‘hello_world'), and open the ‘main.rs‘ file in your favorite text editor. This file contains the main entry point for your Rust program.

Cargo new will automatically build a hello_world application here so for the sake of explaining delete everything in the ‘main.rs‘ folder.

Replace the contents of main.rs with the following code:

fn main() {
    println!("Hello, world!");
}
  • fn -> in rust fn which stands for function is where you write your functions/methods.
  • main() -> in rust like many other languages main is where your primary code will run from. this can obviously be changed but as a starter this is where it runs from.
  • println! -> in rust println is the print function like the Console.WriteLine in C#, Console.Log in Javascript and print in python.
Building and Running your Program

Now, let’s compile and run the program. Navigate back to the root directory of your project (‘hello_world‘) in the terminal, and run the following command:

cargo run

Cargo will compile your code and execute the resulting binary. You should see the “Hello, World!” message printed to the console.

 cargo run
   Compiling hello_world v0.1.0 (\hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 4.79s
     Running `target\debug\hello_world.exe`   
Hello, world!

Conclusion

Congratulations! You’ve successfully built and run your first Rust program. Although this example is simple, it demonstrates the basic syntax and structure of a Rust application. From here, you can explore Rust’s powerful features, such as pattern matching, error handling, and concurrency.

Rust’s official documentation and community resources are excellent places to continue your learning journey in becoming a Rustacean!

Leave a Comment

Your email address will not be published. Required fields are marked *