🚀
2. Concepts
Languages

Programming Concepts

Mar 202510 min read

Variables and Mutability

fn main() {
    let x = 5;
    println!("The value of x is: {}", x);
 
    x = 6;
    println!("The value of x is: {}", x);
}
Execution Output
Error: cannot mutate immutable variable `x`
  • because by default Rust variables are immutable
fn main() {
    let mut x = 5;
    println!("The value of x is: {}", x);
 
    x = 6;
    println!("The value of x is: {}", x);
}
  • adding mut makes the variable mutable

Constants

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
fn main() {
    const mut MAX_POINTS: u32 = 100_000;
}
Execution Output
Error: const globals cannot be mutable

Shadowing

fn main() {
    let x = 5;
    println!("The value of x is: {}", x);
 
    let x = 6;
    println!("The value of x is: {}", x);
}
 
  • x in line 2 is shadowed by x in line 5
  • helps in preserving immutability
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)
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 (opens in a new tab)

fn main() {
    let f: u8 = 255;
    println!("The value of f is: {}", f);
 
    let f: u8 = 256;
    println!("The value of f is: {}", f);
}
Execution Output
Error: literal out of range for `u8`

Tuple

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
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
fn main() {
    let a = [1, 2, 3, 4, 5];
}

Functions

  • Functions are defined using fn keyword
  • Function names follow the snake_case convention
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 -> arrow notation
  • The last expression in the function is the return value
fn main() {
    let x = five();
    println!("The value of x is: {}", x);
}
 
fn five() -> i32 {
    5
}

Control Flow

  • if expression must be explicitly bool type
fn main() {
    let number = 3;
 
    if number < 5 {
        println!("condition was true");
    } else {
        println!("condition was false");
    }
}

Loops

  • loop keyword is used to create an infinite loop
  • break keyword is used to break the loop
fn main() {
    let mut counter = 0;
 
    loop {
        counter += 1;
 
        if counter == 10 {
            break;
        }
    }
}
While Loop
  • while loop is used to loop until a condition is met
fn main() {
    let mut number = 3;
 
    while number != 0 {
        println!("{}!", number);
        number -= 1;
    }
 
    println!("LIFTOFF!!!");
}
For Loop
  • for loop is used to loop over a collection
  • iter method is used to create an iterator over a collection
fn main() {
    let a = [10, 20, 30, 40, 50];
 
    for element in a.iter() {
        println!("the value is: {}", element);
    }
}
For Loop with Range
  • for loop is used to loop over a range
  • rev method is used to reverse the range
fn main() {
    for number in (1..4).rev() {
        println!("{}!", number);
    }
    println!("LIFTOFF!!!");
}

© 2026 Driptanil Datta. All rights reserved.

Software Developer & Engineer

Disclaimer:The content provided on this blog is for educational and informational purposes only. While I strive for accuracy, all information is provided "as is" without any warranties of completeness, reliability, or accuracy. Any action you take upon the information found on this website is strictly at your own risk.

Copyright & IP:Certain technical content, interview questions, and datasets are curated from external educational sources to provide a centralized learning resource. Respect for original authorship is maintained; no copyright infringement is intended. All trademarks, logos, and brand names are the property of their respective owners.

System Operational

Built with Love ❤️ | Last updated: Mar 16 2026