Step 1 of 6
Your first program
A program is a list of instructions the computer follows from top to bottom. Rust is a compiled language: you write the instructions as text (the source code), the Rust compiler (rustc) checks them very carefully and translates them into machine code, and then the program runs. Every time you press Check, a real Rust compiler builds and runs your code.
The shape of every Rust program
fn main() {
println!("Hi!");
}
Hi!
Piece by piece:
fn main()starts a function calledmain.fnis short for function. Every Rust program begins running atmain, no matter how big it is. The empty()say it takes no inputs.- The curly braces
{ }hold the function's body: the instructions that run, in order. println!("Hi!");prints the textHi!and then ends the line ("print line"). The text goes inside double quotes.- The
!meansprintln!is a macro, not a normal function. Macros are Rust code that writes more code for you; you'll meet several (println!,format!,vec!). For now: printing needs the!. - The
;ends the statement, like a period ends a sentence.
Common mistakes
- Forgetting the
!:println("Hi")makes the compiler saycannot find function `println` in this scope. It's a macro, so it'sprintln!. - Spelling and capitals matter:
Println!orprintline!don't exist. - Single quotes:
'Hi'is wrong. Text always goes in double quotes; single quotes are for one character, like'a'.
Your task
Fill in the blank so the program prints Hello, World!.
Challenge 2
Fill in both blanks: the keyword that starts a function, and the name of the function every program starts in. The program should print GG, well played!.
Challenge 3
This time there's no starter program. Write the whole thing yourself: fn main, its braces, and a println!. It should print Welcome to the arena.