Skip to content

Programming Concepts

Updated
17 Sept 2026
3 min read
0 sections
Page 11 of 18
Rust

Variables and Mutability

rust
fn main() {
    let x = 5;
    println!("The value of x is: {}", x);

    x = 6;
    println!("The value of x is: {}", x);
}
Out · error
Error: cannot mutate immutable variable `x`
  • because by default Rust variables are immutable
rust
fn main() {
    let mut x = 5;
    println!("The value of x is: {}", x);

    x = 6;
    println!("The value of x is: {}", x);
}
  • adding mut{:rust} makes the variable mutable

Constants

rust
fn main() {
    const MAX_POINTS: u32 = 100_000;
}
  • Constant variables must be annotated with a type, and it is not inferred with a type at compile time
rust
fn main() {
    const mut MAX_POINTS: u32 = 100_000;
}
Out · error
Error: const globals cannot be mutable

Shadowing

rust
fn main() {
    let x = 5;
    println!("The value of x is: {}", x);

    let x = 6;
    println!("The value of x is: {}", x);
}
  • x{:rust} in line 2 is shadowed by x{:rust} in line 5
  • helps in preserving immutability
rust
fn main() {
    let x = 5;
    println!("The value of x is: {}", x);

    let x = "five";
    println!("The value of x is: {}", x);
}
  • changing the type of the variable

Scalar Datatypes

  • represent a single value (integers, floating-point numbers, Booleans, and characters)
rust
fn main() {
    let x: u8 = 255;
    let y: u16 = 65535;
    let z: u32 = 4_294_967_295;
    let a: u64 = 18_446_744_073_709_551_615;
    let b: u128 = 340_282_366_920_938_463_463_374_607_431_768_211_455;
    let c: usize = usize::MAX;

    let d: i8 = 127;
    let e: i16 = 32_767;
    let f: i32 = 2_147_483_647;
    let g: i64 = 9_223_372_036_854_775_807;
    let h: i128 = 170_141_183_460_469_231_731_687_303_715_884_105_727;
    let i: isize = isize::MAX;

    let j: f32 = f32::MAX;
    let k: f64 = f64::MAX;

    let l:bool = true;
    let m:char = 'a';
    let m:char = '😻';
}

https://doc.rust-lang.org/book/ch03-02-data-types.html

rust
fn main() {
    let f: u8 = 255;
    println!("The value of f is: {}", f);

    let f: u8 = 256;
    println!("The value of f is: {}", f);
}
Out · error
Error: literal out of range for `u8`

Tuple

rust
fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);
}
  • tuple is a collection of multiple values of different types

Destructuring Tuple

  1. destructuring tuple to get individual values
  2. or using dot notation to get individual values
rust
fn main() {
    let tup = (500, 6.4, 1);

    let (x, y, z) = tup;

    let x = tup.0;
}

Array

  • Array is a collection of multiple values of the same type and fixed length
rust
fn main() {
    let a = [1, 2, 3, 4, 5];
}

Functions

  • Functions are defined using fn{:rust} keyword
  • Function names follow the snake_case convention
rust
fn main() {
    print_hello(1000);
}

fn print_hello(number: u16) {
    println!("Hello, {}", number);
}

Statements and Expressions

  • Statements are instructions that perform some action and do not return a value
  • Expressions evaluate to a resulting value

Returning Values from Functions

  • return values using ->{:rust} arrow notation
  • The last expression in the function is the return value
rust
fn main() {
    let x = five();
    println!("The value of x is: {}", x);
}

fn five() -> i32 {
    5
}

Control Flow

  • if{:rust} expression must be explicitly bool{:rust} type
rust
fn main() {
    let number = 3;

    if number < 5 {
        println!("condition was true");
    } else {
        println!("condition was false");
    }
}

Loops

  • loop{:rust} keyword is used to create an infinite loop
  • break{:rust} keyword is used to break the loop
rust
fn main() {
    let mut counter = 0;

    loop {
        counter += 1;

        if counter == 10 {
            break;
        }
    }
}

While Loop

  • while{:rust} loop is used to loop until a condition is met
rust
fn main() {
    let mut number = 3;

    while number != 0 {
        println!("{}!", number);
        number -= 1;
    }

    println!("LIFTOFF!!!");
}

For Loop

  • for{:rust} loop is used to loop over a collection
  • iter{:rust} method is used to create an iterator over a collection
rust
fn main() {
    let a = [10, 20, 30, 40, 50];

    for element in a.iter() {
        println!("the value is: {}", element);
    }
}

For Loop with Range

  • for{:rust} loop is used to loop over a range
  • rev{:rust} method is used to reverse the range
rust
fn main() {
    for number in (1..4).rev() {
        println!("{}!", number);
    }
    println!("LIFTOFF!!!");
}