Day 2: Rust Syntax Basics and Your First Rust Program

·

4 min read

Now that you’ve set up Rust on your machine, it’s time to dive into the basics of Rust syntax. Rust’s syntax is clean and expressive, making it easy to learn for beginners while still being powerful enough for advanced use cases.


1. Basic Rust Syntax

Rust’s syntax is similar to other C-like languages, but it has some unique features. Let’s break it down:

Here's a simple Rust "Hello, World!" program:

fn main() {
    println!("Hello, World!");
}

Explanation:

  1. fn main() {}:

    • This defines the main function, which is the entry point of a Rust program.

    • fn stands for "function," and main is the default function name where execution begins.

  2. println!("Hello, World!");:

    • println! is a Rust macro (indicated by the ! at the end).

    • It prints the given text to the console.

    • The text inside the parentheses is a string enclosed in double quotes ("Hello, World!").

    • The semicolon (;) marks the end of the statement.

Running the Program:

  1. Save the file as main.rs.

  2. Open a terminal and navigate to the file's directory.

  3. Run:

     rustc main.rs
     ./main
    
    • rustc compiles the Rust file.

    • ./main executes the compiled program.

Variables and Mutability
  • Variables in Rust are immutable by default. Use the mut keyword to make them mutable.

      let x = 5; // immutable
      let mut y = 10; // mutable
      y = 15; // allowed
      // x = 20; // this would cause a compile-time error
    
Data Types
  • Rust is a statically typed language, meaning the type of every variable must be known at compile time.

  • Common data types:

    • Integers: i32, u32, i64, etc.

    • Floats: f32, f64

    • Booleans: bool

    • Characters: char

    • Tuples: (i32, f64, char)

    • Arrays: [i32; 5] (fixed-size)

Example:

    let num: i32 = 42;
    let pi: f64 = 3.14;
    let is_rust_fun: bool = true;
    let letter: char = 'R';
    let tuple: (i32, f64, char) = (42, 3.14, 'R');
    let array: [i32; 3] = [1, 2, 3];
Functions
  • Functions are defined using the fn keyword.

  • The return type is specified after an arrow ->.

    Example:

      fn add(a: i32, b: i32) -> i32 {
          a + b // no semicolon means this is the return value
      }
    
Control Flow
  • If-Else:

      let number = 7;
      if number < 5 {
          println!("Condition was true");
      } else {
          println!("Condition was false");
      }
    
  • Loops:

      let mut count = 0;
      loop {
          count += 1;
          if count == 3 {
              break;
          }
      }
    

2. Writing Your First Rust Program

Let’s write a simple program that demonstrates the basics of Rust syntax. This program will:

  1. Declare variables.

  2. Use a function to perform a calculation.

  3. Print the result.

fn main() {
    // Declare variables
    let a = 10;
    let b = 20;

    // Call a function
    let result = add(a, b);

    // Print the result
    println!("The sum of {} and {} is {}", a, b, result);
}

// Function to add two numbers
fn add(x: i32, y: i32) -> i32 {
    x + y
}

Output:

The sum of 10 and 20 is 30

3. Why Is This Important for Web3?

Understanding Rust’s syntax is the foundation for building Web3 applications. For example:

  • Smart Contracts: You’ll use Rust’s syntax to write secure and efficient smart contracts on platforms like Solana.

  • Blockchain Protocols: Rust’s performance and safety features make it ideal for building blockchain protocols.

  • Decentralized Apps (dApps): Rust’s concurrency model helps you build scalable dApps.


4. What’s Next?

In Day 3, we’ll explore Rust’s Ownership Model, one of the most unique and powerful features of Rust. This will help you understand how Rust ensures memory safety without a garbage collector, which is crucial for building secure Web3 applications.


Call to Action

If you’re enjoying this series, share your progress in the comments! Let me know if you have any questions about Rust syntax or Web3. Stay tuned for Day 3! 🚀