Rust Language – 2 – Syntax and Structure of Rust

Syntax and Structure of Rust: Building Secure and Efficient Code

Rust is known for its safety, performance, and modern language features. To harness its power, developers must understand the language’s syntax and structure. In this article, we’ll delve into the key aspects of Rust’s syntax and structure, including variables, data types, control structures, functions, and ownership rules, while providing practical examples to illustrate these concepts.

1. Variables and Mutability

In Rust, variables are declared using the `let` keyword. Variables are immutable by default, meaning their values cannot be changed after assignment. To create mutable variables, you use the `mut` keyword.

Example:
fn main() {
    let x = 5;       // Immutable variable
    let mut y = 10;  // Mutable variable

    y = 15;          // This is allowed
    // x = 7;        // This will result in a compilation error
}
2. Data Types

Rust provides a robust system of data types. The two main categories are scalar and compound data types. Scalar types represent single values, while compound types can group multiple values.

Example:
fn main() {
    let integer: i32 = 42;      // Scalar: Integer
    let float: f64 = 3.14;      // Scalar: Floating-point
    let boolean: bool = true;   // Scalar: Boolean

    let array: [i32; 3] = [1, 2, 3]; // Compound: Array
    let tuple: (i32, f64) = (10, 2.5); // Compound: Tuple
}
3. Control Structures

Rust offers control structures like `if`, `match`, and `loop` for decision-making and looping. These structures enable developers to create complex logic in their code.

Example:
fn main() {
    let number = 7;

    if number % 2 == 0 {
        println!("Even number");
    } else {
        println!("Odd number");
    }

    let grade = 'A';

    match grade {
        'A' => println!("Excellent"),
        'B' => println!("Good"),
        'C' => println!("Average"),
        _ => println!("Need improvement"),
    }

    let mut count = 0;

    loop {
        println!("Count: {}", count);
        count += 1;

        if count == 5 {
            break;
        }
    }
}
4. Functions

Functions in Rust are defined using the `fn` keyword. They can take parameters and return values. Functions are crucial for organizing code and promoting reusability.

Example:
fn main() {
    fn add(a: i32, b: i32) -> i32 {
        a + b
    }

    let result = add(5, 3);
    println!("Result: {}", result);
}
5. Ownership and Borrowing

Rust’s ownership system is a unique feature that enforces strict rules about memory allocation and deallocation. This system ensures memory safety and prevents issues like data races.

Example:
fn main() {
    let s1 = String::from("Hello");
    let s2 = s1; // Ownership transfer

    // This line is not allowed because ownership of s1 has been transferred
    // println!("s1: {}", s1);

    // s2 is now the owner of the string "Hello"
    println!("s2: {}", s2);
}
6. Conclusion

Rust’s syntax and structure are designed to help developers build secure and efficient code. By understanding variables, data types, control structures, functions, and ownership rules, you can leverage Rust’s power to create robust and high-performance software.