Rust Tutorial
Estimated reading: 4 minutes 37 views

🧱 Rust Syntax & Basic Constructs – Master the Foundations of Safe Coding

🧲 Introduction – Why Learn Rust Syntax Early?

Rust’s core strength lies in how its syntax enforces safety, clarity, and performance. Understanding variables, data types, constants, and strings forms the foundation for writing efficient and reliable Rust programs.

🎯 In this guide, you’ll learn:

  • The structure and rules of Rust syntax
  • How variables, constants, and types work
  • How to work with strings, booleans, and formatted output
  • How to write comments and readable code

📘 Topics Covered

🧱 Topic📖 Description
🧾 Rust – SyntaxGeneral rules of Rust syntax and semicolons.
📤 Rust – OutputDisplaying text using println! macro.
💬 Rust – CommentsWriting single-line and multi-line comments.
📦 Rust – VariablesDeclaring mutable and immutable variables.
🧊 Rust – ConstantsCreating immutable, globally accessible values.
🧮 Rust – Data TypesUnderstanding scalar types: integers, floats, chars, and more.
🧵 Rust – StringsUsing &str and String for textual data.
Rust – BooleansUsing bool values in logical operations.

🧾 Rust – Syntax

Rust syntax is strict, concise, and follows the C-like family. Every statement ends with a semicolon (;), and the entry point is the main() function.

fn main() {
    let name = "Rust";
    println!("Hello, {}!", name);
}

🧠 Key Syntax Rules:

  • Curly braces {} define blocks.
  • Semicolons ; end expressions.
  • fn defines a function.
  • let declares a variable.

📤 Rust – Output (Using println! Macro)

Rust uses macros (ending with !) for tasks like printing.

fn main() {
    let age = 30;
    println!("Age is: {}", age);
}

🧠 Explanation:

  • println! is a macro, not a function.
  • {} is a placeholder replaced by the variable age.

💬 Rust – Comments

Write comments to explain your code. Rust supports two styles:

// This is a single-line comment

/*
 This is a
 multi-line comment
*/

💡 Comments are ignored by the compiler but help in readability.


📦 Rust – Variables

Rust variables are immutable by default. Use mut for mutable variables.

fn main() {
    let x = 5;         // immutable
    let mut y = 10;    // mutable
    y = 15;
    println!("x = {}, y = {}", x, y);
}

🧠 Key Rules:

  • Immutable by default for safety.
  • Use let to declare, mut to allow changes.

🧊 Rust – Constants

Constants are declared using const, and they must have a type.

fn main() {
    const PI: f64 = 3.1415;
    println!("Pi = {}", PI);
}

🧠 Notes:

  • Constants are immutable and must be typed.
  • Conventionally written in UPPER_SNAKE_CASE.

🧮 Rust – Data Types

Rust is statically typed. Common scalar types include:

fn main() {
    let integer: i32 = 42;
    let float: f64 = 3.14;
    let is_rust_fun: bool = true;
    let grade: char = 'A';

    println!("int: {}, float: {}, bool: {}, char: {}", integer, float, is_rust_fun, grade);
}

📊 Summary Table:

TypeDescriptionExample
i3232-bit signed integerlet a = 10;
f6464-bit floating-pointlet b = 3.14;
boolBoolean (true / false)let x = true;
charUnicode characterlet c = 'Z';

🧵 Rust – Strings

Rust provides two main types for text:

  1. String literal (&str)
  2. Heap-allocated String (String)
fn main() {
    let s1 = "hello"; // &str
    let s2 = String::from("world");
    println!("{} {}", s1, s2);
}

💡 Use String when you need mutability or runtime modification.

Example – Modifying a String:

fn main() {
    let mut name = String::from("Rust");
    name.push_str(" Lang");
    println!("{}", name); // Rust Lang
}

✅ Rust – Booleans

The bool type can only be true or false. Often used in conditionals.

fn main() {
    let is_safe = true;
    if is_safe {
        println!("Rust is memory safe!");
    }
}

🧠 Booleans are critical for decision making and used heavily in if/else statements.


📌 Summary – Recap & Next Steps

Rust’s syntax prioritizes clarity and safety from the ground up. Mastering these basic constructs prepares you to build reliable and efficient applications.

🔍 Key Takeaways:

  • Use let for variables, mut for mutability.
  • println! outputs formatted text using {}.
  • Rust enforces type safety at compile time.
  • Strings are either &str (static) or String (dynamic).
  • Constants use const and are always typed.

⚙️ Practical Use Cases:

  • Writing readable, safe CLI tools
  • Building low-level modules with precise control
  • Creating efficient web backends with type-safe logic

❓ Frequently Asked Questions

Why are variables immutable by default in Rust?
✅ This encourages safer code by preventing unintended changes.


How do I format text using println!?
✅ Use {} placeholders. Example:

println!("Hello, {}", name);

What’s the difference between &str and String?
&str is a string slice (read-only, fixed), while String is heap-allocated and mutable.


Can I assign different types to a variable later?
✅ No. Rust enforces static typing—you cannot change types once assigned.


Are semicolons mandatory in Rust?
✅ Yes, most lines must end with a semicolon unless it’s the return value of a block.


Share Now :

Leave a Reply

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

Share

Rust Syntax & Basic Constructs

Or Copy Link

CONTENTS
Scroll to Top