Right, so you’ve run cargo new, you’ve got a directory, and you’re staring at src/main.rs. Let’s not just run it; let’s dissect it. This isn’t just a “hello world” program; it’s a Rosetta Stone for understanding how Rust thinks. Every single character here is doing heavy lifting.

Here’s the canonical, almost religiously significant piece of code you’ll find:

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

Let’s break it down line by sinister line.

The main Function: Where It All Begins

Every executable Rust program, from this trivial example to a multi-threaded web server, starts its life at the same exact point: the main function. The fn keyword declares it. The parentheses () indicate it takes no arguments (for now). The curly braces {} define its body.

The runtime doesn’t call _main or Main or __start_here_please. It calls main. This is non-negotiable. It’s the universal constant in the Rust universe. If you rename it, cargo run will happily tell you that you’ve created a library, not a binary, because it can’t find an entry point. It’s brutally literal that way.

The println! Macro: Your First Meet-Cute with Metaprogramming

You’ll notice it’s println! with a bang (!), not println without one. This is your first, and arguably most important, introduction to Rust’s powerful macro system. The bang tells you, “Hey, this isn’t a regular function; this is a macro that’s going to do some clever code generation at compile time.”

Why a macro? Because it’s incredibly flexible. It does string formatting, checks your arguments at compile time (try println!("{}"); without a second argument and see the beautifully terrifying error), and expands into code that’s far more efficient than a simple function could be. It looks like a function call, but it’s really a conversation with the compiler.

Semicolons: The Divisive Punctuation

See that semicolon at the end of the println! line? ; It’s not a suggestion. In Rust, semicolons are how you tell the compiler, “This statement is over, discard its value and move on.” This is crucial for understanding expressions vs. statements later. For now, just know that omitting it will change the meaning of your code in profound ways, usually leading to a compile-time error that will gently (or not so gently) remind you to put it back.

The Indentation: A Matter of Taste, Mostly

You’ll see four-space indentation. This isn’t a Rust mandate; it’s a community convention enforced by rustfmt, the autoformatter. You can configure it to use tabs if you enjoy starting holy wars in your project’s PR comments. The key takeaway is that Rust doesn’t care about whitespace for its grammar (unlike, say, Python), but we care about consistency. Use rustfmt. It settles arguments before they even start.

Let’s Break It and Learn

The best way to understand a system is to see how it fails. Let’s introduce some classic errors.

The Argument Error:

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

Running this will not print “Hello, {}!”. Instead, the compiler will stop you dead in your tracks with a perfectly clear error: argument never used. This is the macro’s compile-time validation at work. It’s saving you from a runtime formatting error.

The Missing Semicolon:

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

This seems innocent, but it will cause an error about mismatched types. Why? Because the main function is expected to return the unit type (), but the println! macro, without a semicolon, is treated as an expression whose value is passed out of the block. The macro’s return type isn’t (), hence the type mismatch. The semicolon throws that value away, returning () instead. It’s a small syntax detail with massive semantic implications.

So there you have it. What looks like a simple one-liner is actually a dense concentration of Rust’s philosophy: explicit entry points, powerful metaprogramming, strict type checking, and a compiler that would rather have a heated argument with you now than a mysterious crash later. It’s not just saying “hello” to the world; it’s introducing you to a language that takes its job very, very seriously.