TExcelTAD
Home › Rust › Rust Syntax

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

rust · try it yourself
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:

rust
fn main() {
    println!("first line")
    println!("second line");
}
text
error: expected `;`, found `println`
 --> src/main.rs:2:27
  |
2 |     println!("first line")
  |                           ^ help: add `;` here

Rust 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:

rust · try it yourself
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:

rust
fn main() { println!("compact"); }
rust
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:

rust · try it yourself
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?

  1. .
  2. :
  3. ;
  4. !

Which naming style is conventional for Rust variables and functions?

  1. camelCase
  2. snake_case
  3. UpperCamelCase
  4. SCREAMING_SNAKE_CASE

What is the value of a block whose last line is a + b (no semicolon)?

  1. Nothing
  2. The value of a + b
  3. Always 0
  4. It does not compile