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

Run the program:

cargo run

This is the normal Rust entry point. println! is a macro that writes formatted output to the terminal.

Variables

Variables are immutable by default. Use mut when the value should change.

let language = "Rust";
let mut count = 1;
count += 1;

Rust values are immutable unless you mark them with mut, which makes state changes explicit.

Ownership

Values have a single owner. Borrow with references when a function should read data without taking ownership.

fn main() {
    let name = String::from("Dan");
    greet(&name);
    println!("{}", name);
}

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

This shows borrowing with &name, so the function can read the value without taking it away from the caller.

Rust Add
fn add(param1: i64, param2: i64) -> i64 {
    param1 + param2
}

This just adds the two input numbers with the language’s normal arithmetic and returns the sum.

Rust Add Border
fn add_border(picture: &[String]) -> Vec<String> {
    let width = picture[0].len();
    let border = "*".repeat(width + 2);

    let mut result = Vec::with_capacity(picture.len() + 2);
    result.push(border.clone());
    for row in picture {
        result.push(format!("*{row}*"));
    }
    result.push(border);

    result
}

This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.

Rust Adjacent Elements Product
fn adjacent_elements_product(input_array: &[i64]) -> i64 {
    input_array
        .windows(2)
        .map(|w| w[0] * w[1])
        .max()
        .unwrap_or(i64::MIN)
}

This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.

Rust Almost Magic Square
fn almost_magic_square(a: &[i64]) -> Vec<i64> {
    let mut grid: Vec<Vec<i64>> = a.chunks(3).map(|c| c.to_vec()).collect();
    let mut row_sum = [0i64; 3];
    let mut col_sum = [0i64; 3];

    for i in 0..3 {
        for j in 0..3 {
            row_sum[i] += grid[i][j];
            col_sum[i] += grid[j][i];
        }
    }

    let mut max_sum = 0;
    for k in 0..3 {
        max_sum = max_sum.max(row_sum[k]);
        max_sum = max_sum.max(col_sum[k]);
    }

    let (mut i, mut j) = (0usize, 0usize);
    while i < 3 && j < 3 {
        let diff = (max_sum - row_sum[i]).min(max_sum - col_sum[j]);
        grid[i][j] += diff;
        row_sum[i] += diff;
        col_sum[j] += diff;

        if row_sum[i] == max_sum {
            i += 1;
        }
        if col_sum[j] == max_sum {
            j += 1;
        }
    }

    grid.into_iter().flatten().collect()
}

This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.

Rust Are Equally Strong
fn are_equally_strong(your_left: i64, your_right: i64, friends_left: i64, friends_right: i64) -> bool {
    your_right.max(your_left) == friends_left.max(friends_right)
        && your_left.min(your_right) == friends_right.min(friends_left)
}

This compares each person’s strongest and weakest arm. If both pairs match, the result is true.

Rust Array Change
fn array_change(mut a: Vec<i64>) -> i64 {
    let mut min = 0;
    for k in 0..a.len().saturating_sub(1) {
        if a[k] >= a[k + 1] {
            let dif = a[k] - a[k + 1] + 1;
            a[k + 1] += dif;
            min += dif;
        }
    }

    min
}

This moves left to right and bumps values only when needed so the array becomes strictly increasing.

Rust Array Maximal Adjacement Difference
fn array_maximal_adjacent_difference(a: &[i64]) -> i64 {
    let mut dif = 0;
    for i in 1..a.len().saturating_sub(1) {
        dif = dif.max((a[i] - a[i - 1]).abs()).max((a[i] - a[i + 1]).abs());
    }

    dif
}

This checks the gap between each pair of neighbors and returns the largest difference.

Rust Binary Gap
fn binary_gap(n: i64) -> i64 {
    let bits = format!("{n:b}");
    let trimmed = bits.trim_matches('0');

    trimmed
        .split('1')
        .map(|zeroes| zeroes.len() as i64)
        .max()
        .unwrap_or(0)
}

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

Rust Bracket
fn bracket(s: &str) -> i64 {
    let mut stack = Vec::new();
    for c in s.chars() {
        match c {
            ')' => {
                if stack.pop() != Some('(') {
                    return 0;
                }
            }
            ']' => {
                if stack.pop() != Some('[') {
                    return 0;
                }
            }
            '}' => {
                if stack.pop() != Some('{') {
                    return 0;
                }
            }
            _ => stack.push(c),
        }
    }

    if stack.is_empty() { 1 } else { 0 }
}

This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.