Rust Syntax

Rust Syntax – Complete Beginner Guide
If you’re starting your Rust journey, understanding Rust syntax is the first major step.
Rust has a clean, modern syntax — but it is also strict and type-safe. That strictness is what makes Rust powerful, fast, and memory-safe.
In this fully beginner guide, you’ll learn:
Basic Rust program structure
Variables and mutability
Data types
Comments
Functions
Control flow (if, loops)
Blocks and scope
Expressions vs statements
Structs and basic patterns
Common syntax mistakes
Let’s dive in
Basic Structure of a Rust Program
Every Rust program starts with a main function:
1 2 3 | fn main() { println!("Hello, Rust!"); } |
Explanation:
fn→ defines a functionmain()→ entry point{}→ block of codeprintln!()→ macro to print text
Rust uses curly braces {} to define blocks.
Comments in Rust
Comments help explain code.
Single-line comment:
Multi-line comment:
This is
a multi-line comment
*/
Comments are ignored during compilation.
Variables in Rust
Rust uses let to declare variables.
1 | let x = 10; |
Rust automatically infers type.
Immutable by Default
By default, variables are immutable:
1 2 | let x = 5; x = 10; // Error |
To make mutable:
1 2 | let mut x = 5; x = 10; // Works |
Rust encourages immutability for safety.
Data Types in Rust
Rust is statically typed.
Integer Types
1 2 | let a: i32 = 10; let b: u32 = 20; |
i32→ signedu32→ unsigned
Floating-Point
1 | let pi: f64 = 3.14; |
Boolean
1 | let is_active: bool = true; |
Character
1 | let letter: char = 'A'; |
Strings in Rust
Rust has two main string types:
String Literal (&str)
1 | let name = "Rust"; |
Owned String (String)
1 2 | let mut name = String::from("Rust"); name.push_str(" Language"); |
Understanding the difference is important.
Functions in Rust
Functions are declared using fn.
1 2 3 | fn greet() { println!("Hello!"); } |
Function with Parameters
1 2 3 | fn add(a: i32, b: i32) -> i32 { a + b } |
Key syntax rules:
Parameter types must be specified
Return type uses
->Last expression returns value
Expressions vs Statements
Rust is expression-based.
Expression (returns value):
1 | let x = 5 + 3; |
Statement (does not return value):
1 | let x = 5; |
Important:
1 2 3 | fn square(x: i32) -> i32 { x * x // no semicolon } |
Adding semicolon turns it into a statement and causes error.
Control Flow – If Statement
1 2 3 4 5 6 7 | let number = 10; if number > 5 { println!("Greater than 5"); } else { println!("Less or equal"); } |
Rust does not require parentheses around condition.
If as Expression
1 | let result = if number > 5 { "Big" } else { "Small" }; |
Both branches must return same type.
Loops in Rust
For Loop
1 2 3 | for i in 1..5 { println!("{}", i); } |
While Loop
1 2 3 4 5 6 | let mut x = 5; while x > 0 { println!("{}", x); x -= 1; } |
Infinite Loop
1 2 3 | loop { println!("Running forever"); } |
Blocks and Scope
Rust uses {} for blocks.
1 2 3 | { let x = 10; } |
Variable exists only inside block.
Shadowing
Rust allows shadowing:
1 2 | let x = 5; let x = x + 1; |
Each let creates new variable.
Different from mutability.
Struct Syntax
Struct defines custom type:
1 2 3 4 | struct Person { name: String, age: u32, } |
Create instance:
1 2 3 4 | let user = Person { name: String::from("Alice"), age: 30, }; |
Access fields:
1 | println!("{}", user.age); |
Match Statement
Rust’s pattern matching:
1 2 3 4 5 6 7 | let number = 3; match number { 1 => println!("One"), 2 => println!("Two"), _ => println!("Other"), } |
_ is default case.
Constants
Use const:
1 | const MAX_POINTS: u32 = 100; |
Rules:
Must specify type
Must be uppercase convention
Importing Modules
Use use keyword:
1 | use std::io; |
Rust uses module system for organization.
Visual Overview of Rust Syntax Structure
Common Beginner Syntax Mistakes
- Forgetting semicolon
- Adding semicolon to return expression
- Forgetting
mutkeyword - Not specifying return type
- Trying C-style syntax
- Confusing
Stringand&str
Rust compiler gives detailed error messages.
Why Rust Syntax Feels Strict
Rust enforces:
Type safety
Ownership rules
Borrow checking
Memory safety
This strictness reduces runtime errors.
Best Practices for Writing Clean Rust Syntax
- Use descriptive variable names
- Keep functions small
- Prefer immutability
- Use
matchinstead of multiple if-else - Let compiler guide you
Frequently Asked Questions (FAQs)
1. Is Rust syntax similar to C++?
Yes, Rust syntax looks similar to C++, but it has stricter safety rules.
2. Why are variables immutable by default?
Rust promotes safety and predictable behavior.
3. Does Rust require semicolons?
Yes, most statements require semicolons.
4. What is the difference between expression and statement?
Expressions return values; statements do not.
5. Is Rust syntax hard to learn?
It can feel strict at first, but becomes clear with practice.
Conclusion
Rust syntax is:
Modern
Clean
Strict
Safe
Expression-based
You learned:
Variables
Data types
Functions
Control flow
Structs
Blocks and scope
Mastering Rust syntax builds a strong foundation for advanced topics like ownership, borrowing, and lifetimes.

