Write Your First Rust Program in Minutes, Hello, World! Made Easy

Published Apr 18, 2024

Here's a simple "Hello, world!" program in Rust:

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

Let's break it down:

  • fn main(): This defines the main function, which is the entry point for your program. Every Rust program needs a main function.
  • println!("Hello, world!");: This line uses the println! macro to print the string "Hello, world!" to the console. The ! at the end indicates it's a macro, not a regular function.

To run this program, you'll need Rust installed on your system. Here are the steps:

  1. Create a file: Save the code above in a file named main.rs.
  2. Compile the code: Open your terminal and navigate to the directory where you saved the file. Then, run the command:
rustc main.rs

This will compile the code and generate an executable file (usually named main or main.exe depending on your OS).
3. Run the program: Execute the generated file:

./main  // For Linux and macOS
./main.exe // For Windows

If everything works correctly, you should see "Hello, world!" printed on your terminal.

This is a very basic example, but it demonstrates the core structure of a Rust program. You can find more information and tutorials on writing Rust programs in the official Rust documentation https://doc.rust-lang.org/rust-by-example/hello.html.

Tags: tech rust Programming Languages Development Beginners Hello World