Sling Academy
Home/Rust/Rust "Hello World" example

Rust "Hello World" example

Last updated: January 02, 2025

The Rust programming language has been gaining momentum as one of the most efficient and performance-oriented languages available today. Known for its safety guarantees and memory management capabilities, Rust is an excellent choice for projects where security and performance are critical. This article will guide you through the process of setting up a simple 'Hello, World!' application using Rust, offering you a gentle introduction to the language and its toolchain.

Setting up Rust

Before we start writing our 'Hello, World!' program, we need to ensure that Rust is installed on your system. The easiest way to install Rust is by using a tool called rustup, a command-line tool for managing Rust versions and associated tools. Open your terminal and enter the following command:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

This command will download and start the installation process. Follow the on-screen instructions to complete the setup. Once completed, you can verify the installation by checking the Rust version:

rustc --version

If installed correctly, this command should display the current version of the rustc compiler.

Your First Rust Program

Now that we have Rust set up, let's write our first 'Hello, World!' program. Open your favorite text editor and create a new file called main.rs. Write the following code in the file:

fn main() {
    println!("Hello, World!");
}

This simple program consists of the main function, which is the entry point of every Rust program. The println! macro is used to print the string "Hello, World!" to the console.

Compiling and Running the Program

Next, we need to compile the Rust program. Navigate to the directory where main.rs is located and run the following command:

rustc main.rs

This will compile the main.rs file, producing an executable file. On Unix-based systems, this executable will simply be main, while on Windows, it will be main.exe.

To run the compiled program, execute the following command:

./main

(On Windows, you should use main.exe instead.) You should see "Hello, World!" printed to your terminal.

Using Cargo to Manage Rust Projects

As your Rust applications grow, manually managing each file and the compilation process can become cumbersome. Cargo, Rust's build system and package manager, simplifies this process by handling dependencies, compilation, and more.

To create a new project with Cargo, run the following command:

cargo new hello_world

Navigate into the newly created directory:

cd hello_world

You’ll notice a generated src directory containing a file named main.rs. This file already contains a 'Hello, World!' implementation. To run the program, use Cargo’s run command:

cargo run

Cargo compiles it if necessary and runs the executable, outputting "Hello, World!"

Conclusion

You've now learned how to install Rust, write a simple 'Hello, World!' program, and use Cargo to manage your Rust projects. These foundational steps are crucial as you continue to explore all that Rust has to offer. As Rust continues to grow, mastering these basics will set you on a solid path in your programming journey.

Next Article: Rust primitive types cheat sheet

Previous Article: What is Rust Cargon?

Series: The basics of Rust

Rust

You May Also Like

  • E0557 in Rust: Feature Has Been Removed or Is Unavailable in the Stable Channel
  • Network Protocol Handling Concurrency in Rust with async/await
  • Using the anyhow and thiserror Crates for Better Rust Error Tests
  • Rust - Investigating partial moves when pattern matching on vector or HashMap elements
  • Rust - Handling nested or hierarchical HashMaps for complex data relationships
  • Rust - Combining multiple HashMaps by merging keys and values
  • Composing Functionality in Rust Through Multiple Trait Bounds
  • E0437 in Rust: Unexpected `#` in macro invocation or attribute
  • Integrating I/O and Networking in Rust’s Async Concurrency
  • E0178 in Rust: Conflicting implementations of the same trait for a type
  • Utilizing a Reactor Pattern in Rust for Event-Driven Architectures
  • Parallelizing CPU-Intensive Work with Rust’s rayon Crate
  • Managing WebSocket Connections in Rust for Real-Time Apps
  • Downloading Files in Rust via HTTP for CLI Tools
  • Mocking Network Calls in Rust Tests with the surf or reqwest Crates
  • Rust - Designing advanced concurrency abstractions using generic channels or locks
  • Managing code expansion in debug builds with heavy usage of generics in Rust
  • Implementing parse-from-string logic for generic numeric types in Rust
  • Rust.- Refining trait bounds at implementation time for more specialized behavior