0.1 Rust Syntax

Different programming languages use different words and symbols to express similar ideas. The rules that define which words and symbols can be used, and how they can be combined, are called syntax. We'll now explore some basic language elements of the Rust programming language.

Output

In order to see what our programs do we need a way to output data. On way to achieve this is to print text to the screen. For this, Rust provides the print!() and println!() commands. The difference between them is that println! adds a line break after the output, while print! does not. The text you want to output needs to be enclosed in quotation marks "".

print!("Hello, world!");

Write greetings in different languages:

println!("Hello, world!");
println!("Привіт, світе!");
println!("世界、こんにちは。");

When using print!, the outputs appear on the same line:

print!("Hello, world!");
print!("Привіт, світе!");
print!("世界、こんにちは。");

Use a different way to represent »Hello«:

println!("X    X  XXXXXX  X      X       XXXXX ");
println!("X    X  X       X      X      X     X");
println!("XXXXXX  XXXXX   X      X      X     X");
println!("X    X  X       X      X      X     X");
println!("X    X  XXXXXX  XXXXX  XXXXX   XXXXX ");

Comments

Comments allow you to add notes to your code that are ignored when the program runs but can help humans understand the code better. In Rust, comments start with // and continue to the end of the line. Comments are especially useful for explaining what the code does or why certain decisions were made. Sometimes, they can also be used to temporarily disable parts of the code.

// Print "Hello, world!" in three different languages
println!("Hello, world!");
println!("Привіт, світе!"); // Ukrainian
// println!("世界、こんにちは。");

Variables

Variables are a way to store values in a program so the same value can be used again later. Here is how to create a variable in Rust:

let name = "Jane";

// variables can be printed like this:
println!("Hello, {}!", name);
// ...or like this:
println!("Bye, {ame}!");

A bit contradictory to their name ›variables‹ (and to their behavior in many other programming languages), variables in Rust cannot be changed unless they are explicitly marked as mutable:

let mut age = 30; // mutable variable
println!("Age: {}", age);
age = 31;
println!("New age: {age}");

Trying to change an ›immutable‹ variable will result in an error by the Rust compiler and an explanation of why the code is invalid:

let age = 30; // immutable variable
println!("Age: {}", age);
age = 31; // This line will cause an error
println!("New age: {age}");

Operators

let x = 3;
let y = 5;
let xy = x * y;

println!("{x} * {y} = {xy}");
println!("{} * {} = {}", x, y, xy);
println!("{} * {} = {}", x, y, x * y);
println!("{x} * {y} = {}", x * y);

Rust supports the four basic arithmetic operations: addition (+), subtraction (-), multiplication (*), and division (/).

let x = 10;
let y = 2;

println!("Sum:        {x} + {y} = {}", x + y);
println!("Difference: {x} - {y} = {}", x - y);
println!("Product:    {x} * {y} = {}", x * y);
println!("Quotient:   {x} / {y} = {}", x / y);

Expressions & Statements

An expression in Rust is a piece of code that produces a value. The number 3 is an expression that produces the value 3. The operation 7 - 4 is another expression that produces the value 3. Expressions can be combined to create more complex expressions. For example, (3 + 4) * 2 is an expression that produces the value 14. Parentheses () can be used to group expressions and control the order in which the expressions are evaluated. Variables are also expressions because they produce the value they store. The same is true for operations on variables, such as x * y.

The values produced by expression can be output by print! and println!. The placeholder {} is used to indicate where the value should be inserted in the output text. Variables (but not other expressions) can also be put directly into the output string like, e.g. {x}. The value produced by an expression can also be assigned to a variable for later use.

let x = 3;
let y = 5;

// Output the value produced by an expression
println!("The product is: {}", x * y);

// Store the value produced by an expression in a variable
let xy = x * y;
println!("The product is: {xy}");

A statement in Rust is a piece of code that performs an action but does not produce a value. For example, the line let x = 3; is a statement that declares a variable x and assigns it the value 3. Statements do not produce values and cannot be used as part of expressions. In Rust, statements typically end with a semicolon ;.

Compilation errors

The Rust compiler checks your code for errors before it runs. If there are errors in your code, the compiler will produce error messages that explain what went wrong. Here are some examples of common errors:

// greet the world
println!("Hello, world!");

Text not marked as comment or quoted text will cause an error:

greet the world
println!(Hello, world!);
let x = 3;
let y = 5;

// Output the value produced by an expression
println!("The product is: {}", x * y);

Missing semicolons at the end of statements will cause an error. The compiler will refuse to compile the code if there is single error even if other parts of the code are correct:

let x = 3;
let y = 5

// Output the value produced by an expression
println!("The product is: {}", x * y);

Trying to change an immutable variable will cause an error:

let x = 3;
x = 4;

Other Languages

Other programming languages use slightly different syntax for the same concepts. Here are some examples:

Rust

// Assign values
let x = 3;
let mut y = 4;

// Change y to 5
y = 5;

// Compute product
let xy = x * y;

// Print the result
println!("{x} * {y} = {xy}");

C++

// Assign values
auto x = 3;
auto y = 4;

// Change y to 5
y = 5;

// Compute product
auto xy = x * y;

// Print the result
println("{} * {} = {}", x, y, xy);

Go

// Assign values
x := 3
y := 4

// Change y to 5
y = 5

// Compute product
xy := x * y

// Print the result
fmt.Printf("%d * %d = %d\n", x, y, xy)

Python

# Assign values
x = 3
y = 4

# Change y to 5
y = 5

# Compute product
xy = x * y

# Print the result
print(f"{x} * {y} = {xy}")
winfried2026-01-16 13:46:43.513646000