bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Rust/Rust Tutorial
Rust•Rust Tutorial

Rust Syntax

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.

Previous

Rust Get Started

Next

Rust Output (Print Text)

This chapter

Overview
20

Lessons

46m

Read time

1. Rust Tutorial2. Rust Introduction3. Rust Get Started4. Rust Syntax5. Rust Output (Print Text)6. Rust Comments7. Rust Variables8. Rust Data Types9. Rust Constants10. Rust Operators11. Rust Booleans12. Rust If .. Else Conditions13. Rust Match14. Rust Loops15. Rust While Loops16. Rust For Loop17. Rust Functions18. Rust Scope19. Rust Strings20. Rust Ownership