Main
fn main() {
// your code here
}
• main
is the starting point of every Rust program. It’s where the program begins running.
• Rust automatically looks for a function named main()
when you run a program.
• That’s why it must be called main
— you can’t rename it.
• it is required in executable programs
But what is the parenthesis ()
after main
for ?
• These are function parameters.
fn main() {
// your code here
}
The ()
means:
This function takes no input (no parameters)
You can't define main with an input or parameter
I.e
fn main(input: &str) {
// ❌ This is invalid in Rust
}
That’s because Rust expects main()
to always match a specific signature — it must look like:
fn main() {
// ✅ This is valid
}
But functions can take input. And when they do, the input goes inside those parentheses.
For example: Example 1
fn main() {
greet("Kehn");
}
fn greet(name: &str) {
println!("👋🏼 Hello, {}!", name);
}
Here's what's happening:
Rust requires all instructions to run from inside a function, and that function must start from main()
.
that's why we had to write main and call the greet function inside it first
In the line greet("Kehn");
, you are calling the function greet
, and passing "Kehn"
as the input to it.
🧠 Let's imagine the compiler is a robot that follows rules:
It does not read your code from top to bottom like a storybook.
Instead, it asks:
"Where is main()
? That’s my entry point. I’ll start there and run whatever is inside it."
If you just write greet("Kehn");
outside of any function, Rust gets confused:
❌ "Wait, when should I run this? How? I don't just run random lines of code lying outside — that's illegal in Rustland!"
• greet()
is defined down below
fn greet(name: &str) {
println!("👋🏼 Hello, {}!", name);
}
— It's like saying: "Here's how you greet someone."
• But unless someone calls it from inside main()
(the starting point), it won't do anything.
Just defining greet() doesn't tell the computer to run it. It's just there, waiting to be used.
Now let's dive into Example 1 bit by bit
✅ Line 1: fn main() {
🔤 What it means:
• fn
→ This means function.
• main
→ The name of the function. Rust always looks for main()
first. It's the entry point.
• ()
→ These are parentheses. This function takes no input.
• {
→ This starts the body of the function. All the code that the function runs will go inside these curly braces {}
.
🧠 So what is happening?
We’re telling Rust: "Hey, this is the main starting point. Whatever is inside these curly braces should run first."
✅ Line 2: greet("Kehn");
🔤 What it means:
• greet
→ This is the name of a function we’re calling (we'll define it later).
• ("Kehn")
→ We're passing a value — the text "Kehn"
— into that function.
• ;
→ This ends the line. Rust needs this semicolon to know the instruction is complete.
🧠 So what is happening?
Inside
main()
, we are saying: “Hey Rust, please run the greet function and give it the word"Kehn"
.”
✅ Line 3: }
This ends the main()
function. It says: “That’s it for main. No more instructions here.”
✅ Line 4: fn greet(name: &str) {
🔤 What it means:
fn
→ Again, this is a function definition.
greet
→ This is the name of the function.
name: &str
→ We are saying:
This function takes one parameter called name, and it must be a string slice (a bit of text).
More specifically:
• name
→ This is the name of the variable the function will use.
• &str
→ This is the type of that variable. It means: “a reference to a string” → like "Kehn"
or "Rustacean"
.
• {
→ Starts the body of the greet function.
🧠 So what is happening?
We’re defining a function called greet that expects a name as input. Then, we’ll do something with that name — like printing it out.
✅ Line 5: println!("👋🏼 Hello, {}!", name);
🔤 What it means:
• println!
→ This is a macro that prints a line of text to the screen.
• !
→ Means it's a macro, not a normal function. Macros do some extra behind-the-scenes magic.
• "👋🏼 Hello, {}!"
→ This is the text we want to print. The {} is a placeholder — we’ll fill it in.
• , name
→ We are saying: “Put the value of name (which is "Kehn"
) inside the {}.”
• ;
→ End of the instruction.
🧠 So what is happening?
Rust prints:
👋🏼 Hello, Kehn!
Because we passed "Kehn"
into greet, and greet used println!
to insert that value.
✅ Line 6: }
This ends the greet()
function.
Don't worry if you're still confused about lines 4 and 5 – we'll go through them in more detail in the next episodes.
🎯 Final Flow Summary:
Rust starts with
main()
.main()
says: “Call thegreet()
function and give it"Kehn"
.”Rust jumps to
greet()
and sets name ="Kehn"
.greet()
prints:
👋🏼 Hello, Kehn!
Top comments (1)
Next episode! 🙂
Some comments may only be visible to logged-in visitors. Sign in to view all comments.