Rust Syntax & Basic Constructs
Estimated reading: 3 minutes 370 views

🦀 Rust – Syntax: Understanding the Basics of Writing Rust Code

Introduction – Why Learn Rust Syntax?

Before you dive into ownership or concurrency, it’s crucial to master Rust’s syntax fundamentals. The syntax is expressive yet strict—ensuring readability, safety, and performance from the ground up. Unlike C or Python, Rust blends low-level control with modern structure.

In this guide, you’ll learn:

  • The basic syntax rules and structure of Rust
  • How to declare variables, data types, and functions
  • Proper use of semicolons, braces, and macros
  • Common pitfalls to avoid as a beginner

Basic Structure of a Rust Program

Here’s a minimal Rust program structure:

fn main() {
    println!("Hello, Rust!");
}
ElementRole
fnDefines a function
main()Program entry point
{}Denotes function block
println!()Macro to print text to standard output
;Terminates most expressions in Rust

Key Syntax Rules in Rust

ConceptSyntax ExampleNotes
Comments// single-line/* multi-line */Compiler ignores comments
Statement terminatorEnd most lines with a semicolon ;Required for expressions
Block delimiterUse {} for code blocksMandatory for fn, if, loop, etc.
Case sensitivitylet namelet NameRust is case-sensitive
IndentationTypically 4 spaces (no tabs preferred)Not enforced by compiler, but used by convention

Variable Declaration Syntax

fn main() {
    let language = "Rust";
    let mut version = 2025;
    const PI: f32 = 3.14;

    println!("{} {}", language, version);

    version = 2026; // allowed because it's mutable
    println!("Updated: {}", version);
}

Syntax Points:

  • let – immutable by default
  • mut – allows mutation
  • const – must include type and be uppercase

Function Syntax in Rust

fn greet(name: &str) {
    println!("Hello, {}!", name);
}

fn main() {
    greet("Rustacean");
}
PartSyntaxMeaning
Function keywordfnDefines a function
Parametersname: &strReference to a string slice
Call expressiongreet("Rustacean")Calls the function

Functions return () (unit type) by default if no return is specified.


If-Else and Match Syntax

If-Else

fn main() {
    let temp = 32;

    if temp > 30 {
        println!("Hot!");
    } else {
        println!("Cool!");
    }
}

Match (Rust’s switch)

fn main() {
    let grade = 'A';

    match grade {
        'A' => println!("Excellent"),
        'B' => println!("Good"),
        _ => println!("Needs Improvement"),
    }
}

Loop Syntax Overview

TypeSyntax ExampleDescription
looploop { break; }Infinite loop, must break manually
whilewhile condition { ... }Checks condition before loop body
forfor i in 0..5 { ... }Iterates over a range or iterator

Example:

fn main() {
    for i in 1..=3 {
        println!("i = {}", i);
    }
}

Output:

i = 1
i = 2
i = 3

Common Syntax Mistakes to Avoid

MistakeResult/ErrorFix
Forgetting ;May trigger unexpected behaviorEnd each statement with ;
Using print() instead of println!()Function not found or syntax errorUse macro with !: println!()
Mutating an immutable variableCompiler error about mutabilityDeclare with mut
Returning a value without returnWorks but confusing in complex logicUse return value; or last expression

Summary – Recap & Next Steps

Rust’s syntax is clean, consistent, and built for performance with safety. Once you internalize its structure—functions, blocks, macros, variables—you’ll be writing expressive and error-free code more confidently.

Key Takeaways:

  • Rust uses braces and semicolons like C, but with stricter rules
  • Macros like println! end with ! and are common in Rust
  • let, mut, and const form the backbone of declarations
  • Control flow uses if, match, loop, while, and for

Next, explore Rust – Output to master how to format and print data beautifully.


FAQs


Does Rust use semicolons like C/C++?
Yes. Almost every expression must end with a semicolon unless it’s a return value in a block.


Why does println in Rust end with !?
println! is a macro, not a function. Macros in Rust are invoked using !.


Is indentation important in Rust like Python?
No, but proper indentation (typically 4 spaces) is a strong convention and improves readability.


Can I skip the main() function in small programs?
No. Rust always requires a main() function as the program’s entry point.


Share Now :
Share

Rust – Syntax

Or Copy Link

CONTENTS
Scroll to Top