All lessons

Your First Program

6 min · Rust

Write and run your very first Rust program, right here on the page.

Rust · Your First Program0/5 cleared

Enemy 1 of 5

Every Rust program needs a starting point, and that starting point is always a function called main. When you run a Rust program, the operating system hands control to fn main() first — nothing happens before it, and (in the normal case) nothing happens after it returns. The curly braces { } mark the block of code that belongs to that function; everything you want to run lives between them.

println! is how you print text to the screen. Look closely at the exclamation mark right after println — that's not a typo and not decoration. It marks println! as a macro rather than an ordinary function. Macros run at compile time and can do things ordinary functions can't, like accepting a flexible, checked format string. You'll see the same ! on other built-in macros later (like vec! for building lists), so get used to spotting it.

The text you want printed goes inside parentheses and double quotes: println!("Hello, World!"). Whatever sits between the quotes is printed exactly as written, including spaces and punctuation. Forget the semicolon at the end of the line and Rust's compiler will stop you with an error — in Rust, semicolons end most statements, unlike in Python or Kotlin where a line break is often enough.

You
Binary Treant

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: Hello, World!