Your First Program
Every coder in the whole world remembers their first program. Itβs a tiny one that simply says hello. Today, itβs your turn to write it! π
The front door: fn main()
Every Rust program needs a starting point β a place where the computer begins reading
your instructions. That place is called main. We write it like this: fn main().
fn main() is the front door of your program. When the
computer runs your code, it always walks in through that door first.
The instructions you want to run go inside the curly braces { } β like items packed
neatly inside a backpack.
Saying hello with println!
To put words on the screen, we use println!. It means βprint a line.β You give
it some words inside quotation marks, and it shows them for everyone to see.
! after println tells us it's a macro β
a special built-in helper that does extra work for you behind the scenes.
Notice the little semicolon ; at the end of the line. In Rust, a semicolon means
βthis instruction is finished.β Itβs like the period at the end of a sentence.
Run it yourself!
Hereβs the whole program. Press the green βΆ Run button and watch it say hello!
You did it! Those words came straight from instructions you ran. π
println!("I love Rust!");
Press βΆ Run to see both lines appear, one after the other.
; at the end of each line, and keep your words
snug inside the quotation marks. Rust likes things tidy!
Quick quiz
What does println! do?
Exactly! println! means "print a line" β it shows your words on the screen.
fn main(), println!
is a macro that prints words (the ! gives it away), and each
line ends with a semicolon ;. Next up: meet Cargo, your robot helper! π€