Flash cards
Review the key moves
What is the main idea behind Rust Syntax?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
___!("Hello World!");Put the learning moves in the order that makes the concept easiest to apply.
Syntax
You have already seen the following code a couple of times in the first chapters. Let's break it down to understand it better:
Example
fn main() {
println!("Hello World!");
}Example explained
Line 1: fn main() is something that always appears at the beginning of every Rust program. main() is called a function , and any code inside its curly brackets {} will be executed.
Line 2: println!() is a macro , used to output/print text to the screen. In our example it will output "Hello World!". To end the code, you must remember a semicolon ( ; ).
What is a macro?
A macro is like a function, but with an exclamation mark ( ! ) after it. Don't worry about the terminology for now. For now, just know that macros are similar to functions (they execute things), but they do not always follow the same rules as functions. You will learn more about macros later.
Good to know: The Rust compiler ignores white spaces.
The code above could also been written as: fn main(){println!("Hello World!");}
However , multiple lines and indentation makes the code more readable.