Roadmap to Rustacean ๐Ÿฆ€

Roadmap to Rustacean ๐Ÿฆ€

ยท

4 min read

Why Learn Rust?

  1. Memory Safety: Rust ensures memory safety without a garbage collector.

  2. Concurrency: Rust enables safe and efficient concurrent programming.

  3. Performance: Rust offers performance comparable to C and C++.

  4. Community: Rust has a vibrant community and ecosystem supported by Cargo.

  5. Versatility: Rust is versatile and supports cross-platform development.

Installing Rust on Windows

Step 1: Quick Install

Step 2: let's Get Rusty

If you get some error rustup and cargo not found make sure to install the respective packages according to your operating systems

Hello World In Rust ๐Ÿฆ€

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

Data Types In Rust ๐Ÿฆ€

  • Primitive Data Types:

    • Integer Types: Represent whole numbers. Examples include i32, u64, i8, etc.

    • Floating-Point Types: Represent numbers with fractional parts. Examples include f64 and f32.

    • Boolean Type: Represents true or false values. Denoted by bool.

    • Character Type: Represents a single Unicode character. Denoted by char.

  • Compound Data Types:

    • Arrays: Fixed-size collections of elements of the same type. Denoted by [T; N], where T is the type of elements and N is the number of elements.

    • Tuples: Fixed-size collections of elements of possibly different types. Denoted by (T1, T2, ...).

fn main() {
    // Primitive data types
    let integer: i32 = 42;
    let floating_point: f64 = 3.14;
    let boolean: bool = true;
    let character: char = 'A';

    println!("Integer: {}", integer);
    println!("Floating point: {}", floating_point);
    println!("Boolean: {}", boolean);
    println!("Character: {}", character);

    // Compound data types
    let array: [i32; 3] = [1, 2, 3];
    let tuple: (i32, f64, bool) = (10, 3.14, false);

    println!("Array: {:?}", array);
    println!("Tuple: {:?}", tuple);

    // Accessing elements in array and tuple
    println!("First element of array: {}", array[0]);
    println!("Second element of tuple: {}", tuple.1);
}

Conditional Statements In Rust ๐Ÿฆ€

fn main() {
    let number = 10;

    // Simple if-else statement
    if number > 0 {
        println!("Number is positive");
    } else {
        println!("Number is non-positive");
    }

    // if-else if-else statement
    if number > 0 {
        println!("Number is positive");
    } else if number < 0 {
        println!("Number is negative");
    } else {
        println!("Number is zero");
    }

    // Using if in let statement (if as an expression)
    let is_positive = if number > 0 {
        true
    } else {
        false
    };

    println!("Is number positive? {}", is_positive);
}

Operators in Rust ๐Ÿฆ€

fn main() {
    // Arithmetic operators
    let a = 10;
    let b = 5;
    println!("Addition: {}", a + b);
    println!("Subtraction: {}", a - b);
    println!("Multiplication: {}", a * b);
    println!("Division: {}", a / b);
    println!("Remainder: {}", a % b);

    // Comparison operators
    let x = 10;
    let y = 20;
    println!("Equal to: {}", x == y);
    println!("Not equal to: {}", x != y);
    println!("Greater than: {}", x > y);
    println!("Less than: {}", x < y);
    println!("Greater than or equal to: {}", x >= y);
    println!("Less than or equal to: {}", x <= y);

    // Logical operators
    let p = true;
    let q = false;
    println!("Logical AND: {}", p && q);
    println!("Logical OR: {}", p || q);
    println!("Logical NOT: {}", !p);

    // Bitwise operators
    let m = 0b1010; // Binary representation of 10
    let n = 0b1100; // Binary representation of 12
    println!("Bitwise AND: {:b}", m & n);
    println!("Bitwise OR: {:b}", m | n);
    println!("Bitwise XOR: {:b}", m ^ n);
    println!("Bitwise NOT: {:b}", !m);

    // Assignment operators
    let mut c = 5;
    c += 3; // Equivalent to c = c + 3
    println!("Compound Assignment: {}", c);

    // Increment and decrement operators
    let mut d = 10;
    d += 1; // Increment
    println!("Increment: {}", d);
    d -= 1; // Decrement
    println!("Decrement: {}", d);

    // Range operator
    for i in 1..=5 {
        println!("Inclusive Range: {}", i);
    }
}

Pointers in Rust ๐Ÿฆ€

fn main() {
    let x = 42;

    // Reference: Immutable reference to x
    let reference_to_x = &x;
    println!("Value of x via reference: {}", *reference_to_x);

    // Mutable reference: Mutable reference to x
    let mut y = 10;
    let mutable_reference_to_y = &mut y;
    *mutable_reference_to_y += 5;
    println!("Value of y via mutable reference: {}", *mutable_reference_to_y);

    // Raw pointer: Immutable raw pointer to x
    let raw_pointer_to_x: *const i32 = &x;
    unsafe {
        println!("Value of x via raw pointer: {}", *raw_pointer_to_x);
    }

    // Raw pointer: Mutable raw pointer to y
    let mut z = 20;
    let raw_pointer_to_mut_z: *mut i32 = &mut z;
    unsafe {
        *raw_pointer_to_mut_z += 10;
        println!("Value of z via mutable raw pointer: {}", *raw_pointer_to_mut_z);
    }
}

Control Flow ๐Ÿฆ€

// Match Expressions
let number = 5;
match number {
    1 => println!("One"),
    2 => println!("Two"),
    3..=5 => println!("Three to Five"),
    _ => println!("Other"),
}
// Loop Statements
let mut count = 0;
loop {
    println!("Count: {}", count);
    count += 1;
    if count >= 5 {
        break;
    }
}
// For loop
for number in 1..=5 {
    println!("Number: {}", number);
}
// Enhanced For Loop

Did you find this article valuable?

Support Thirumalai by becoming a sponsor. Any amount is appreciated!

ย