Basic Syntax in Rust
In this lesson, you will learn the basic syntax rules of the Rust programming language. Understanding Rust syntax is essential before moving on to variables, data types, and ownership concepts.
Rust syntax is designed to be explicit, safe, and readable. It avoids ambiguity and enforces clear structure in programs.
Rust Program Structure
A Rust program is made up of functions, statements, and expressions.
Every Rust program starts execution from the main function.
Here is the simplest Rust program:
fn main() {
println!("Hello, Rust!");
}
This program prints a message to the console.
The main Function
The main function is the entry point of a Rust program.
Every executable Rust program must have exactly one main function.
Syntax:
fn main() {
// program logic
}
The keyword fn is used to define a function in Rust.
Statements and Expressions
Rust distinguishes between statements and expressions.
- Statements perform actions and do not return values
- Expressions evaluate to a value
Example:
fn main() {
let x = 5; // statement
let y = x + 1; // expression used in a statement
println!("{}", y);
}
In Rust, most things are expressions, which makes the language powerful and flexible.
Semicolons in Rust
Semicolons play an important role in Rust syntax.
- A semicolon ends a statement
- An expression without a semicolon returns a value
Example:
fn main() {
let x = {
let a = 3;
a + 1 // no semicolon, returns value
};
println!("{}", x);
}
Removing or adding semicolons can change program behavior.
Comments in Rust
Comments are used to explain code and are ignored by the compiler.
Rust supports single-line comments using //.
// This is a comment
fn main() {
// This line prints a message
println!("Rust is awesome!");
}
Comments improve readability and maintainability of code.
Printing Output
Rust provides macros for printing output to the console.
println!prints text with a new lineprint!prints text without a new line
Example:
fn main() {
print!("Hello ");
println!("Rust!");
}
Code Blocks
A code block in Rust is defined using curly braces { }.
Code blocks group statements and expressions together.
Example:
fn main() {
{
let x = 10;
println!("{}", x);
}
}
Variables declared inside a block are only accessible within that block.
Whitespace and Formatting
Rust ignores extra whitespace, but consistent formatting improves readability.
Rust provides an official formatting tool called rustfmt.
You can format your code using:
cargo fmt
This tool automatically formats code according to Rust standards.
📝 Practice Exercises
Exercise 1
Write a Rust program that prints your name.
Exercise 2
Create a block expression that returns a number and print it.
Exercise 3
Add comments to explain each line of a simple Rust program.
✅ Practice Answers
Answer 1
fn main() {
println!("My name is Rustacean");
}
Answer 2
fn main() {
let value = {
let a = 5;
a * 2
};
println!("{}", value);
}
Answer 3
// Entry point of the program
fn main() {
// Print a message
println!("Learning Rust");
}
What’s Next?
Now that you understand Rust’s basic syntax, you are ready to learn about variables and mutability.
In the next lesson, you will explore how Rust handles variables, immutability, and mutable data safely.