Rust Syntax

Rust Tutorial

🦀 Rust Syntax

Rust syntax is clean, strict, and expressive. The compiler enforces rules that help you write safe and reliable code.

🔹 1. Basic Structure of a Rust Program


 

Explanation

  • fndefines a function

  • main() → entry point of the program

  • { } → code block

  • println! → macro for printing output

  • ; → statement terminator


🔹 2. Comments in Rust

// Single-line comment

/*
Multi-line
comment
*/


🔹 3. Variables & Mutability

By default, variables are immutable.


 


🔹 4. Data Types (Basic Syntax)


 

Rust is statically typed, but it can infer types automatically.


🔹 5. Print Output


 


🔹 6. If–Else Statement


 

⚠️ Condition must be boolean (no 1 or 0 like C).


🔹 7. Looping Syntax

▶️ loop (infinite loop)


 

▶️ while loop


 

▶️ for loop


 


🔹 8. Functions


 

Calling function:


 


🔹 9. match Statement (Very Powerful)


 


🔹 10. Statements vs Expressions


 


🔑 Important Syntax Rules

  • Every statement ends with ;

  • No null values (uses Option)

  • Curly braces {} are mandatory

  • No implicit type conversion

  • Compiler errors are very descriptive

You may also like...