Getting Started with Rust: The Hello, World! Example

Getting Started with Rust: The Hello, World! Example

The Hello, World! example in Rust serves as an introduction for beginners, showcasing the fundamental structure of a Rust program and demonstrating how to print output to the console.

Key Concepts

  • Rust Basics: Rust is a systems programming language emphasizing safety and performance.
  • Main Function: Every Rust program contains a main function, which acts as the entry point.
  • Printing to Console: The println! macro is utilized to print text to the console.

Structure of a Rust Program

Here’s how a simple Rust program looks:

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

Breakdown of the Example

  • fn main():
    • fn indicates a function declaration.
    • main is the name of the function that executes first.
  • {}:
    • Curly braces define the function's body.
  • println!("Hello, World!");:
    • println! is a macro (indicated by the !), used for printing formatted strings to the console.
    • The text inside the quotes is what will be displayed.

Key Points

  • Macros: Rust employs macros (like println!) to deliver functionality that can be expanded at compile time.
  • String Literals: The text "Hello, World!" is a string literal, representing a fixed string value.

Running the Program

To run the program:

  1. Save the code in a file named main.rs.
  2. Use the Rust compiler (rustc) to compile the program:
  3. Execute the generated binary:
  4. You should see the output:
Hello, World!
./main
rustc main.rs

Conclusion

The Hello, World! program serves as a foundational starting point for learning Rust, illustrating the syntax and structure of a basic Rust application. Grasping this example establishes a solid foundation for more complex programming concepts in Rust.