Rust Syntax
Syntax is the set of rules that decides how Rust code must be written so the compiler can understand it. Rust's syntax looks familiar if you have seen C, Java or JavaScript, but it has a few details of its own. Let's take a small program apart piece by piece.
Anatomy of a Rust program
fn main() {
let planet = "Mars";
println!("Next stop: {}", planet);
}Next stop: Mars
| Part | Meaning |
|---|---|
fn |
Keyword that starts a function definition |
main |
The name of the function. main is special: the program starts here |
() |
The parameter list. main takes no parameters, so it is empty |
{ ... } |
Curly braces surround the body of the function |
let planet = "Mars"; |
A statement that creates a variable |
println!(...) |
A macro call that prints a line of text |
; |
Ends a statement |
Statements and semicolons
Most lines inside a function are statements, and a statement ends with a semicolon ;. If you forget one, the compiler stops and tells you exactly where it expected it:
fn main() {
println!("first line")
println!("second line");
}error: expected `;`, found `println`
--> src/main.rs:2:27
|
2 | println!("first line")
| ^ help: add `;` hereRust also has expressions, which produce a value. A block's last expression without a semicolon becomes the value of that block. You will see this again in the Functions chapter, but here is a taste:
fn main() {
let total = {
let apples = 3;
let pears = 4;
apples + pears
};
println!("Fruit in the basket: {}", total);
}Fruit in the basket: 7
The inner block ends with apples + pears and no semicolon, so its value (7) is stored in total.
Whitespace and indentation
Rust ignores extra spaces, tabs and line breaks. These two programs are identical to the compiler:
fn main() { println!("compact"); }fn main() {
println!("compact");
}The convention is to indent with four spaces and to put the opening brace on the same line as fn. The rustfmt tool (cargo fmt) formats code this way automatically, so most Rust code in the world looks alike.
Case sensitivity and naming
Rust is case sensitive: score, Score and SCORE are three different names. The community follows strong naming conventions, and the compiler even warns you if you break them:
| Kind of item | Style | Example |
|---|---|---|
| Variables and functions | snake_case |
user_name, print_report |
| Types (structs, enums, traits) | UpperCamelCase |
BankAccount |
| Constants and statics | SCREAMING_SNAKE_CASE |
MAX_USERS |
Several functions
A program can contain many functions. Their order in the file does not matter — main can call a function defined below it:
fn main() {
greet();
farewell();
}
fn greet() {
println!("Good morning!");
}
fn farewell() {
println!("See you later!");
}Good morning!
See you later!
Tip: When the compiler reports an error, read the whole message. Rust's errors point to the exact line and column, and often include a
help:line with the fix.
Test yourself
What character ends most statements in Rust?
- .
- :
- ;
- !
Which naming style is conventional for Rust variables and functions?
- camelCase
- snake_case
- UpperCamelCase
- SCREAMING_SNAKE_CASE
What is the value of a block whose last line is a + b (no semicolon)?
- Nothing
- The value of
a + b - Always 0
- It does not compile