Rust Syntax

Rust Tutorial

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:


 

Explanation:

  • fn → defines a function

  • main() → entry point

  • {} → block of code

  • println!() → macro to print text

Rust uses curly braces {} to define blocks.


 Comments in Rust

Comments help explain code.

Single-line comment:

// This is a comment

Multi-line comment:

/*
This is
a multi-line comment
*/

Comments are ignored during compilation.


Variables in Rust

Rust uses let to declare variables.


 

Rust automatically infers type.


Immutable by Default

By default, variables are immutable:


 

To make mutable:


 

Rust encourages immutability for safety.


 Data Types in Rust

Rust is statically typed.

Integer Types


 

  • i32 → signed

  • u32 → unsigned


Floating-Point


 


Boolean


 


Character


 


 Strings in Rust

Rust has two main string types:

String Literal (&str)


 

Owned String (String)


 

Understanding the difference is important.


Functions in Rust

Functions are declared using fn.


 


Function with Parameters


 

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


 

Statement (does not return value):


 

Important:


 

Adding semicolon turns it into a statement and causes error.


 Control Flow – If Statement


 

Rust does not require parentheses around condition.


If as Expression


 

Both branches must return same type.


 Loops in Rust

For Loop


 


While Loop


 


Infinite Loop


 


 Blocks and Scope

Rust uses {} for blocks.


 

Variable exists only inside block.


Shadowing

Rust allows shadowing:


 

Each let creates new variable.

Different from mutability.


 Struct Syntax

Struct defines custom type:


 

Create instance:


 

Access fields:


 


Match Statement

Rust’s pattern matching:


 

_ is default case.


Constants

Use const:


 

Rules:

  • Must specify type

  • Must be uppercase convention


Importing Modules

Use use keyword:


 

Rust uses module system for organization.


Visual Overview of Rust Syntax Structure

Rust Syntax

Common Beginner Syntax Mistakes

  •  Forgetting semicolon
  •  Adding semicolon to return expression
  •  Forgetting mut keyword
  •  Not specifying return type
  •  Trying C-style syntax
  •  Confusing String and &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 match instead 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.

You may also like...