Topic
Statements & Expressions
Every line of Rust is either a statement (does something, returns nothing) or an expression (evaluates to a value). The semicolon is the switch between them.
Quick Reference
| Concept | What it means |
|---|---|
| Statement | A line that ends with ; and returns nothing |
| Expression | A line that evaluates to a value, no semicolon |
| ; | Turns an expression into a statement, discards its value |
| { } | A block of code, returns the value of its last expression |
| () | The unit type, what you get when a block or function returns nothing |
| Term | What it means | When you use it |
|---|---|---|
| Statement | A line that performs an action but produces no value, ends with ; | Variable declarations, assignments, most lines of code |
| Expression | A line that evaluates to a value, no semicolon at the end | The last line of a block or function that you want to return |
| ; | The semicolon, turns an expression into a statement and discards the value | After every line that does not return a value |
| { } | A block, can be an expression if its last line has no semicolon | Grouping code; the last expression becomes the block's value |
| () | The unit type, what you get when a semicolon discards an expression | Return type of functions that return nothing |