Skip to main content

Your First Rust Program: 'Hello, World!' (Part 2)

In the previous article, we created and ran our first Rust program using cargo run. Now, it's time to understand the code we ran. We will dissect the "Hello, World!" program line by line to understand the basic structure of a Rust application.


📚 Prerequisites

Before we begin, you should have:

  • Created a "Hello, World!" project with cargo new.
  • An understanding of what a function is in programming.

🎯 Article Outline: What You'll Master

In this article, you will learn:

  • The role of the main function.
  • The anatomy of a Rust function.
  • What a macro is and how println! works.
  • The importance of comments in Rust.

🧠 Section 1: Dissecting src/main.rs

Let's look at the code in src/main.rs again:

// This is the main function.
fn main() {
// Print text to the console.
println!("Hello, world!");
}

This small program demonstrates several important features of Rust. Let's break it down.


💻 Section 2: The main Function

The first part of the program is the definition of the main function:

fn main() {
// ...
}
  • fn: This is the keyword used to declare a new function in Rust.
  • main: This is the special name for the function that serves as the entry point of every executable Rust program. When you run your program, the code inside the main function is the first code that is executed.
  • (): These parentheses indicate that the main function has no parameters.
  • {}: The curly braces define the body of the function. All the code that belongs to the function goes inside these braces.

It is a requirement that every binary (executable) crate has a main function.


🚀 Section 3: The println! Macro

Inside the main function, we have a single line of code:

println!("Hello, world!");

This line does all the work of printing our message to the screen. There are two important things to notice here:

  1. println! is a macro, not a function. You can tell it's a macro because of the exclamation mark !. We will cover the difference between functions and macros in detail later in the series. For now, just know that macros are a way of writing code that writes other code (this is called metaprogramming).
  2. "Hello, world!" is a string literal. This is the argument we are passing to the println! macro. The macro will print this string to the console.
  3. The line ends with a semicolon ;. Rust is an expression-based language, but most lines of code end with a semicolon. This indicates that the expression is finished and the next one can begin.

📝 Section 4: Comments

The "Hello, World!" program generated by Cargo also includes comments:

// This is the main function.
// Print text to the console.

A comment is a note that the compiler ignores but that people reading the code can see. It's a way to explain what your code is doing. In Rust, comments start with two slashes // and continue to the end of the line.

Writing good comments is an important part of writing high-quality code. They help other developers (and your future self) understand your code.


✨ Conclusion & Key Takeaways

You now understand the fundamental building blocks of a simple Rust program. You've learned about the main function, the println! macro, and comments.

Let's summarize the key takeaways:

  • Every executable Rust program starts in the main function.
  • You declare functions with the fn keyword.
  • println! is a macro that prints text to the console.
  • Comments are ignored by the compiler but are essential for human readers.

Challenge Yourself: Add another println! line to your main function to print a second message. What happens when you run cargo run?


➡️ Next Steps

This concludes our first series, "What is Rust?". You've learned about Rust's philosophy, its ecosystem, how to set up your environment, and the basic structure of a Rust program.

In our next series, "Understanding Basic Rust Syntax and Data Types", we will start to explore the building blocks of the language, beginning with variables and mutability.

You've laid the foundation. Now it's time to start building.


Glossary (Rust Terms)

  • Function: A block of code that performs a specific task.
  • Macro: A way of writing code that writes other code, also known as metaprogramming.
  • String Literal: A string value that is hardcoded in the source code.

Further Reading (Rust Resources)