`fn main()` — What a Rust Program Looks Like - Day 04
On this page
Open src/main.rs and you’ll see this:
Familiar? It should be. But there are a few things worth noticing if you’re coming from Java.
No class wrapper #
In Java, everything lives inside a class — even main:
In Rust, fn main() is a free function. No class, no public static, no String[] args unless you need it. Less ceremony, more signal.
Semicolons matter — but not like in Java #
Rust uses semicolons to turn an expression into a statement. This might sound trivial, but it’s actually meaningful:
If you add a semicolon to the last line, the function returns () (nothing) instead of the value. The compiler will tell you immediately.
fn — declaring functions #
Parameter types go after the name (name: &str), not before like in Java. Return type comes after ->. You’ll get used to it quickly.
Everything is an expression #
This is a core Rust idea. if, match, and blocks all return values. You can write:
let message = if true else ;
No ternary operator needed. The block itself is the value.