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.