Chapter 4: Rust Syntax

Rust Syntax now, which is the next logical step after installation, introduction, and getting started.

Today we’re going deep into how Rust looks and feels when you write code — the grammar, the rules, the little symbols that make Rust unique. I’ll explain it like a real teacher sitting with you: slowly, with many examples, analogies from everyday life (and Indian context 😄), comparisons to other languages you might know (Python, Java, C++), and code you can copy-paste + run right now.

Rust syntax is clean but strict. It borrows ideas from C/C++ (curly braces, semicolons), but adds modern safety features (no null, pattern matching everywhere, powerful macros). Most of the “weird” parts exist to enforce ownership & borrowing at compile time — that’s the magic that makes Rust safe without slowing down.

Let’s break it down section by section — start simple, build up.

1. Basic Structure — Hello World & Entry Point

Every Rust binary starts with main():

Rust
  • fn = keyword for function (like def in Python, void in C++)
  • main() = special name — Rust always looks for this as starting point
  • { } = curly braces define block/scope (same as C-family)
  • println! = macro (notice the !) — prints with newline. Macro = code that writes code (super powerful, like templates)
  • ; = ends almost every statement (forget it → compiler yells nicely)

Run with cargo run — output:

text

2. Variables & Mutability (Biggest difference from Python)

Variables are immutable by default — huge safety win!

Rust
  • let = declare variable (no var, const separate)
  • mut = only if you plan to change it (prevents accidental bugs)
  • Types inferred automatically most times (great productivity)
  • Shadowing ≠ mutation — it’s a new variable with same name (cleans up code)

3. Data Types & Primitives (Quick overview)

Rust has strong, static typing — but inference hides it often.

Rust

No null — use Option<T> instead (we’ll see later).

4. Strings — Two kinds (very important)

Rust
  • &str = view/slice (cheap, common in function args)
  • String = owns data (can grow, like Python str but explicit)

5. Functions & Parameters (Clean & explicit)

Rust
  • Return type after ->
  • Last line without ; = return value
  • &str = borrow (no ownership transfer)

6. Control Flow — if, loop, match (match is king!)

Rust

Match — Rust’s most loved feature (like super-powered switch)

Rust

Exhaustive — compiler forces you to handle all cases!

7. Ownership Syntax in Action (The heart — quick intro)

Rust
  • No & = move ownership
  • & = immutable borrow
  • &mut = mutable borrow (only one at a time!)

8. Structs & Enums (Custom types)

Rust

Quick Tips from 2026 Teacher

  • Indentation = 4 spaces (official)
  • snake_case for variables/functions, CamelCase for types
  • ! = macro
  • ? = error propagation (in functions returning Result)
  • Compiler errors are your friend — very detailed & helpful

This covers ~80% of day-to-day syntax. The rest (traits, generics, lifetimes ‘a, async/await) come later.

Want to focus next on one part?

  • Match & patterns in detail?
  • How ownership syntax feels in bigger examples?
  • Struct + impl (methods)?
  • Or run a bigger code example together?

Tell me — I’m your Rust guru today! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *